34fe571e15ee2cf482a503b8ffe9fad2c0090642
[safe/jmp/linux-2.6] / fs / gfs2 / ops_inode.c
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
4  *
5  * This copyrighted material is made available to anyone wishing to use,
6  * modify, copy, or redistribute it subject to the terms and conditions
7  * of the GNU General Public License version 2.
8  */
9
10 #include <linux/slab.h>
11 #include <linux/spinlock.h>
12 #include <linux/completion.h>
13 #include <linux/buffer_head.h>
14 #include <linux/namei.h>
15 #include <linux/utsname.h>
16 #include <linux/mm.h>
17 #include <linux/xattr.h>
18 #include <linux/posix_acl.h>
19 #include <linux/gfs2_ondisk.h>
20 #include <linux/crc32.h>
21 #include <linux/lm_interface.h>
22 #include <asm/uaccess.h>
23
24 #include "gfs2.h"
25 #include "incore.h"
26 #include "acl.h"
27 #include "bmap.h"
28 #include "dir.h"
29 #include "eaops.h"
30 #include "eattr.h"
31 #include "glock.h"
32 #include "inode.h"
33 #include "meta_io.h"
34 #include "ops_dentry.h"
35 #include "ops_inode.h"
36 #include "quota.h"
37 #include "rgrp.h"
38 #include "trans.h"
39 #include "util.h"
40
41 /**
42  * gfs2_create - Create a file
43  * @dir: The directory in which to create the file
44  * @dentry: The dentry of the new file
45  * @mode: The mode of the new file
46  *
47  * Returns: errno
48  */
49
50 static int gfs2_create(struct inode *dir, struct dentry *dentry,
51                        int mode, struct nameidata *nd)
52 {
53         struct gfs2_inode *dip = GFS2_I(dir);
54         struct gfs2_sbd *sdp = GFS2_SB(dir);
55         struct gfs2_holder ghs[2];
56         struct inode *inode;
57
58         gfs2_holder_init(dip->i_gl, 0, 0, ghs);
59
60         for (;;) {
61                 inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0);
62                 if (!IS_ERR(inode)) {
63                         gfs2_trans_end(sdp);
64                         if (dip->i_alloc->al_rgd)
65                                 gfs2_inplace_release(dip);
66                         gfs2_quota_unlock(dip);
67                         gfs2_alloc_put(dip);
68                         gfs2_glock_dq_uninit_m(2, ghs);
69                         mark_inode_dirty(inode);
70                         break;
71                 } else if (PTR_ERR(inode) != -EEXIST ||
72                            (nd && (nd->intent.open.flags & O_EXCL))) {
73                         gfs2_holder_uninit(ghs);
74                         return PTR_ERR(inode);
75                 }
76
77                 inode = gfs2_lookupi(dir, &dentry->d_name, 0, nd);
78                 if (inode) {
79                         if (!IS_ERR(inode)) {
80                                 gfs2_holder_uninit(ghs);
81                                 break;
82                         } else {
83                                 gfs2_holder_uninit(ghs);
84                                 return PTR_ERR(inode);
85                         }
86                 }
87         }
88
89         d_instantiate(dentry, inode);
90
91         return 0;
92 }
93
94 /**
95  * gfs2_lookup - Look up a filename in a directory and return its inode
96  * @dir: The directory inode
97  * @dentry: The dentry of the new inode
98  * @nd: passed from Linux VFS, ignored by us
99  *
100  * Called by the VFS layer. Lock dir and call gfs2_lookupi()
101  *
102  * Returns: errno
103  */
104
105 static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
106                                   struct nameidata *nd)
107 {
108         struct inode *inode = NULL;
109
110         dentry->d_op = &gfs2_dops;
111
112         inode = gfs2_lookupi(dir, &dentry->d_name, 0, nd);
113         if (inode && IS_ERR(inode))
114                 return ERR_CAST(inode);
115
116         if (inode) {
117                 struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
118                 struct gfs2_holder gh;
119                 int error;
120                 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
121                 if (error) {
122                         iput(inode);
123                         return ERR_PTR(error);
124                 }
125                 gfs2_glock_dq_uninit(&gh);
126                 return d_splice_alias(inode, dentry);
127         }
128         d_add(dentry, inode);
129
130         return NULL;
131 }
132
133 /**
134  * gfs2_link - Link to a file
135  * @old_dentry: The inode to link
136  * @dir: Add link to this directory
137  * @dentry: The name of the link
138  *
139  * Link the inode in "old_dentry" into the directory "dir" with the
140  * name in "dentry".
141  *
142  * Returns: errno
143  */
144
145 static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
146                      struct dentry *dentry)
147 {
148         struct gfs2_inode *dip = GFS2_I(dir);
149         struct gfs2_sbd *sdp = GFS2_SB(dir);
150         struct inode *inode = old_dentry->d_inode;
151         struct gfs2_inode *ip = GFS2_I(inode);
152         struct gfs2_holder ghs[2];
153         int alloc_required;
154         int error;
155
156         if (S_ISDIR(inode->i_mode))
157                 return -EPERM;
158
159         gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
160         gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
161
162         error = gfs2_glock_nq_m(2, ghs);
163         if (error)
164                 goto out;
165
166         error = permission(dir, MAY_WRITE | MAY_EXEC, NULL);
167         if (error)
168                 goto out_gunlock;
169
170         error = gfs2_dir_check(dir, &dentry->d_name, NULL);
171         switch (error) {
172         case -ENOENT:
173                 break;
174         case 0:
175                 error = -EEXIST;
176         default:
177                 goto out_gunlock;
178         }
179
180         error = -EINVAL;
181         if (!dip->i_inode.i_nlink)
182                 goto out_gunlock;
183         error = -EFBIG;
184         if (dip->i_di.di_entries == (u32)-1)
185                 goto out_gunlock;
186         error = -EPERM;
187         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
188                 goto out_gunlock;
189         error = -EINVAL;
190         if (!ip->i_inode.i_nlink)
191                 goto out_gunlock;
192         error = -EMLINK;
193         if (ip->i_inode.i_nlink == (u32)-1)
194                 goto out_gunlock;
195
196         alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
197         if (error < 0)
198                 goto out_gunlock;
199         error = 0;
200
201         if (alloc_required) {
202                 struct gfs2_alloc *al = gfs2_alloc_get(dip);
203                 if (!al) {
204                         error = -ENOMEM;
205                         goto out_gunlock;
206                 }
207
208                 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
209                 if (error)
210                         goto out_alloc;
211
212                 error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
213                 if (error)
214                         goto out_gunlock_q;
215
216                 al->al_requested = sdp->sd_max_dirres;
217
218                 error = gfs2_inplace_reserve(dip);
219                 if (error)
220                         goto out_gunlock_q;
221
222                 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
223                                          al->al_rgd->rd_length +
224                                          2 * RES_DINODE + RES_STATFS +
225                                          RES_QUOTA, 0);
226                 if (error)
227                         goto out_ipres;
228         } else {
229                 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
230                 if (error)
231                         goto out_ipres;
232         }
233
234         error = gfs2_dir_add(dir, &dentry->d_name, ip, IF2DT(inode->i_mode));
235         if (error)
236                 goto out_end_trans;
237
238         error = gfs2_change_nlink(ip, +1);
239
240 out_end_trans:
241         gfs2_trans_end(sdp);
242 out_ipres:
243         if (alloc_required)
244                 gfs2_inplace_release(dip);
245 out_gunlock_q:
246         if (alloc_required)
247                 gfs2_quota_unlock(dip);
248 out_alloc:
249         if (alloc_required)
250                 gfs2_alloc_put(dip);
251 out_gunlock:
252         gfs2_glock_dq_m(2, ghs);
253 out:
254         gfs2_holder_uninit(ghs);
255         gfs2_holder_uninit(ghs + 1);
256         if (!error) {
257                 atomic_inc(&inode->i_count);
258                 d_instantiate(dentry, inode);
259                 mark_inode_dirty(inode);
260         }
261         return error;
262 }
263
264 /**
265  * gfs2_unlink - Unlink a file
266  * @dir: The inode of the directory containing the file to unlink
267  * @dentry: The file itself
268  *
269  * Unlink a file.  Call gfs2_unlinki()
270  *
271  * Returns: errno
272  */
273
274 static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
275 {
276         struct gfs2_inode *dip = GFS2_I(dir);
277         struct gfs2_sbd *sdp = GFS2_SB(dir);
278         struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
279         struct gfs2_holder ghs[3];
280         struct gfs2_rgrpd *rgd;
281         struct gfs2_holder ri_gh;
282         int error;
283
284         error = gfs2_rindex_hold(sdp, &ri_gh);
285         if (error)
286                 return error;
287
288         gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
289         gfs2_holder_init(ip->i_gl,  LM_ST_EXCLUSIVE, 0, ghs + 1);
290
291         rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
292         gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
293
294
295         error = gfs2_glock_nq(ghs); /* parent */
296         if (error)
297                 goto out_parent;
298
299         error = gfs2_glock_nq(ghs + 1); /* child */
300         if (error)
301                 goto out_child;
302
303         error = gfs2_glock_nq(ghs + 2); /* rgrp */
304         if (error)
305                 goto out_rgrp;
306
307         error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
308         if (error)
309                 goto out_rgrp;
310
311         error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
312         if (error)
313                 goto out_rgrp;
314
315         error = gfs2_dir_del(dip, &dentry->d_name);
316         if (error)
317                 goto out_end_trans;
318
319         error = gfs2_change_nlink(ip, -1);
320
321 out_end_trans:
322         gfs2_trans_end(sdp);
323         gfs2_glock_dq(ghs + 2);
324 out_rgrp:
325         gfs2_holder_uninit(ghs + 2);
326         gfs2_glock_dq(ghs + 1);
327 out_child:
328         gfs2_holder_uninit(ghs + 1);
329         gfs2_glock_dq(ghs);
330 out_parent:
331         gfs2_holder_uninit(ghs);
332         gfs2_glock_dq_uninit(&ri_gh);
333         return error;
334 }
335
336 /**
337  * gfs2_symlink - Create a symlink
338  * @dir: The directory to create the symlink in
339  * @dentry: The dentry to put the symlink in
340  * @symname: The thing which the link points to
341  *
342  * Returns: errno
343  */
344
345 static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
346                         const char *symname)
347 {
348         struct gfs2_inode *dip = GFS2_I(dir), *ip;
349         struct gfs2_sbd *sdp = GFS2_SB(dir);
350         struct gfs2_holder ghs[2];
351         struct inode *inode;
352         struct buffer_head *dibh;
353         int size;
354         int error;
355
356         /* Must be stuffed with a null terminator for gfs2_follow_link() */
357         size = strlen(symname);
358         if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
359                 return -ENAMETOOLONG;
360
361         gfs2_holder_init(dip->i_gl, 0, 0, ghs);
362
363         inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
364         if (IS_ERR(inode)) {
365                 gfs2_holder_uninit(ghs);
366                 return PTR_ERR(inode);
367         }
368
369         ip = ghs[1].gh_gl->gl_object;
370
371         ip->i_di.di_size = size;
372
373         error = gfs2_meta_inode_buffer(ip, &dibh);
374
375         if (!gfs2_assert_withdraw(sdp, !error)) {
376                 gfs2_dinode_out(ip, dibh->b_data);
377                 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
378                        size);
379                 brelse(dibh);
380         }
381
382         gfs2_trans_end(sdp);
383         if (dip->i_alloc->al_rgd)
384                 gfs2_inplace_release(dip);
385         gfs2_quota_unlock(dip);
386         gfs2_alloc_put(dip);
387
388         gfs2_glock_dq_uninit_m(2, ghs);
389
390         d_instantiate(dentry, inode);
391         mark_inode_dirty(inode);
392
393         return 0;
394 }
395
396 /**
397  * gfs2_mkdir - Make a directory
398  * @dir: The parent directory of the new one
399  * @dentry: The dentry of the new directory
400  * @mode: The mode of the new directory
401  *
402  * Returns: errno
403  */
404
405 static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
406 {
407         struct gfs2_inode *dip = GFS2_I(dir), *ip;
408         struct gfs2_sbd *sdp = GFS2_SB(dir);
409         struct gfs2_holder ghs[2];
410         struct inode *inode;
411         struct buffer_head *dibh;
412         int error;
413
414         gfs2_holder_init(dip->i_gl, 0, 0, ghs);
415
416         inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
417         if (IS_ERR(inode)) {
418                 gfs2_holder_uninit(ghs);
419                 return PTR_ERR(inode);
420         }
421
422         ip = ghs[1].gh_gl->gl_object;
423
424         ip->i_inode.i_nlink = 2;
425         ip->i_di.di_size = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
426         ip->i_di.di_flags |= GFS2_DIF_JDATA;
427         ip->i_di.di_entries = 2;
428
429         error = gfs2_meta_inode_buffer(ip, &dibh);
430
431         if (!gfs2_assert_withdraw(sdp, !error)) {
432                 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
433                 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
434                 struct qstr str;
435
436                 gfs2_str2qstr(&str, ".");
437                 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
438                 gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
439                 dent->de_inum = di->di_num; /* already GFS2 endian */
440                 dent->de_type = cpu_to_be16(DT_DIR);
441                 di->di_entries = cpu_to_be32(1);
442
443                 gfs2_str2qstr(&str, "..");
444                 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
445                 gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
446
447                 gfs2_inum_out(dip, dent);
448                 dent->de_type = cpu_to_be16(DT_DIR);
449
450                 gfs2_dinode_out(ip, di);
451
452                 brelse(dibh);
453         }
454
455         error = gfs2_change_nlink(dip, +1);
456         gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
457
458         gfs2_trans_end(sdp);
459         if (dip->i_alloc->al_rgd)
460                 gfs2_inplace_release(dip);
461         gfs2_quota_unlock(dip);
462         gfs2_alloc_put(dip);
463
464         gfs2_glock_dq_uninit_m(2, ghs);
465
466         d_instantiate(dentry, inode);
467         mark_inode_dirty(inode);
468
469         return 0;
470 }
471
472 /**
473  * gfs2_rmdir - Remove a directory
474  * @dir: The parent directory of the directory to be removed
475  * @dentry: The dentry of the directory to remove
476  *
477  * Remove a directory. Call gfs2_rmdiri()
478  *
479  * Returns: errno
480  */
481
482 static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
483 {
484         struct gfs2_inode *dip = GFS2_I(dir);
485         struct gfs2_sbd *sdp = GFS2_SB(dir);
486         struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
487         struct gfs2_holder ghs[3];
488         struct gfs2_rgrpd *rgd;
489         struct gfs2_holder ri_gh;
490         int error;
491
492
493         error = gfs2_rindex_hold(sdp, &ri_gh);
494         if (error)
495                 return error;
496         gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
497         gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
498
499         rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
500         gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
501
502         error = gfs2_glock_nq_m(3, ghs);
503         if (error)
504                 goto out;
505
506         error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
507         if (error)
508                 goto out_gunlock;
509
510         if (ip->i_di.di_entries < 2) {
511                 if (gfs2_consist_inode(ip))
512                         gfs2_dinode_print(ip);
513                 error = -EIO;
514                 goto out_gunlock;
515         }
516         if (ip->i_di.di_entries > 2) {
517                 error = -ENOTEMPTY;
518                 goto out_gunlock;
519         }
520
521         error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
522         if (error)
523                 goto out_gunlock;
524
525         error = gfs2_rmdiri(dip, &dentry->d_name, ip);
526
527         gfs2_trans_end(sdp);
528
529 out_gunlock:
530         gfs2_glock_dq_m(3, ghs);
531 out:
532         gfs2_holder_uninit(ghs);
533         gfs2_holder_uninit(ghs + 1);
534         gfs2_holder_uninit(ghs + 2);
535         gfs2_glock_dq_uninit(&ri_gh);
536         return error;
537 }
538
539 /**
540  * gfs2_mknod - Make a special file
541  * @dir: The directory in which the special file will reside
542  * @dentry: The dentry of the special file
543  * @mode: The mode of the special file
544  * @rdev: The device specification of the special file
545  *
546  */
547
548 static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
549                       dev_t dev)
550 {
551         struct gfs2_inode *dip = GFS2_I(dir);
552         struct gfs2_sbd *sdp = GFS2_SB(dir);
553         struct gfs2_holder ghs[2];
554         struct inode *inode;
555
556         gfs2_holder_init(dip->i_gl, 0, 0, ghs);
557
558         inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
559         if (IS_ERR(inode)) {
560                 gfs2_holder_uninit(ghs);
561                 return PTR_ERR(inode);
562         }
563
564         gfs2_trans_end(sdp);
565         if (dip->i_alloc->al_rgd)
566                 gfs2_inplace_release(dip);
567         gfs2_quota_unlock(dip);
568         gfs2_alloc_put(dip);
569
570         gfs2_glock_dq_uninit_m(2, ghs);
571
572         d_instantiate(dentry, inode);
573         mark_inode_dirty(inode);
574
575         return 0;
576 }
577
578 /**
579  * gfs2_rename - Rename a file
580  * @odir: Parent directory of old file name
581  * @odentry: The old dentry of the file
582  * @ndir: Parent directory of new file name
583  * @ndentry: The new dentry of the file
584  *
585  * Returns: errno
586  */
587
588 static int gfs2_rename(struct inode *odir, struct dentry *odentry,
589                        struct inode *ndir, struct dentry *ndentry)
590 {
591         struct gfs2_inode *odip = GFS2_I(odir);
592         struct gfs2_inode *ndip = GFS2_I(ndir);
593         struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
594         struct gfs2_inode *nip = NULL;
595         struct gfs2_sbd *sdp = GFS2_SB(odir);
596         struct gfs2_holder ghs[5], r_gh;
597         struct gfs2_rgrpd *nrgd;
598         unsigned int num_gh;
599         int dir_rename = 0;
600         int alloc_required;
601         unsigned int x;
602         int error;
603
604         if (ndentry->d_inode) {
605                 nip = GFS2_I(ndentry->d_inode);
606                 if (ip == nip)
607                         return 0;
608         }
609
610         /* Make sure we aren't trying to move a dirctory into it's subdir */
611
612         if (S_ISDIR(ip->i_inode.i_mode) && odip != ndip) {
613                 dir_rename = 1;
614
615                 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE, 0,
616                                            &r_gh);
617                 if (error)
618                         goto out;
619
620                 error = gfs2_ok_to_move(ip, ndip);
621                 if (error)
622                         goto out_gunlock_r;
623         }
624
625         num_gh = 1;
626         gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
627         if (odip != ndip) {
628                 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
629                 num_gh++;
630         }
631         gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
632         num_gh++;
633
634         if (nip) {
635                 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
636                 num_gh++;
637                 /* grab the resource lock for unlink flag twiddling 
638                  * this is the case of the target file already existing
639                  * so we unlink before doing the rename
640                  */
641                 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
642                 if (nrgd)
643                         gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
644         }
645
646         error = gfs2_glock_nq_m(num_gh, ghs);
647         if (error)
648                 goto out_uninit;
649
650         /* Check out the old directory */
651
652         error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
653         if (error)
654                 goto out_gunlock;
655
656         /* Check out the new directory */
657
658         if (nip) {
659                 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
660                 if (error)
661                         goto out_gunlock;
662
663                 if (S_ISDIR(nip->i_inode.i_mode)) {
664                         if (nip->i_di.di_entries < 2) {
665                                 if (gfs2_consist_inode(nip))
666                                         gfs2_dinode_print(nip);
667                                 error = -EIO;
668                                 goto out_gunlock;
669                         }
670                         if (nip->i_di.di_entries > 2) {
671                                 error = -ENOTEMPTY;
672                                 goto out_gunlock;
673                         }
674                 }
675         } else {
676                 error = permission(ndir, MAY_WRITE | MAY_EXEC, NULL);
677                 if (error)
678                         goto out_gunlock;
679
680                 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
681                 switch (error) {
682                 case -ENOENT:
683                         error = 0;
684                         break;
685                 case 0:
686                         error = -EEXIST;
687                 default:
688                         goto out_gunlock;
689                 };
690
691                 if (odip != ndip) {
692                         if (!ndip->i_inode.i_nlink) {
693                                 error = -EINVAL;
694                                 goto out_gunlock;
695                         }
696                         if (ndip->i_di.di_entries == (u32)-1) {
697                                 error = -EFBIG;
698                                 goto out_gunlock;
699                         }
700                         if (S_ISDIR(ip->i_inode.i_mode) &&
701                             ndip->i_inode.i_nlink == (u32)-1) {
702                                 error = -EMLINK;
703                                 goto out_gunlock;
704                         }
705                 }
706         }
707
708         /* Check out the dir to be renamed */
709
710         if (dir_rename) {
711                 error = permission(odentry->d_inode, MAY_WRITE, NULL);
712                 if (error)
713                         goto out_gunlock;
714         }
715
716         alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
717         if (error < 0)
718                 goto out_gunlock;
719         error = 0;
720
721         if (alloc_required) {
722                 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
723                 if (!al) {
724                         error = -ENOMEM;
725                         goto out_gunlock;
726                 }
727
728                 error = gfs2_quota_lock(ndip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
729                 if (error)
730                         goto out_alloc;
731
732                 error = gfs2_quota_check(ndip, ndip->i_inode.i_uid, ndip->i_inode.i_gid);
733                 if (error)
734                         goto out_gunlock_q;
735
736                 al->al_requested = sdp->sd_max_dirres;
737
738                 error = gfs2_inplace_reserve(ndip);
739                 if (error)
740                         goto out_gunlock_q;
741
742                 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
743                                          al->al_rgd->rd_length +
744                                          4 * RES_DINODE + 4 * RES_LEAF +
745                                          RES_STATFS + RES_QUOTA + 4, 0);
746                 if (error)
747                         goto out_ipreserv;
748         } else {
749                 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
750                                          5 * RES_LEAF + 4, 0);
751                 if (error)
752                         goto out_gunlock;
753         }
754
755         /* Remove the target file, if it exists */
756
757         if (nip) {
758                 if (S_ISDIR(nip->i_inode.i_mode))
759                         error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
760                 else {
761                         error = gfs2_dir_del(ndip, &ndentry->d_name);
762                         if (error)
763                                 goto out_end_trans;
764                         error = gfs2_change_nlink(nip, -1);
765                 }
766                 if (error)
767                         goto out_end_trans;
768         }
769
770         if (dir_rename) {
771                 struct qstr name;
772                 gfs2_str2qstr(&name, "..");
773
774                 error = gfs2_change_nlink(ndip, +1);
775                 if (error)
776                         goto out_end_trans;
777                 error = gfs2_change_nlink(odip, -1);
778                 if (error)
779                         goto out_end_trans;
780
781                 error = gfs2_dir_mvino(ip, &name, ndip, DT_DIR);
782                 if (error)
783                         goto out_end_trans;
784         } else {
785                 struct buffer_head *dibh;
786                 error = gfs2_meta_inode_buffer(ip, &dibh);
787                 if (error)
788                         goto out_end_trans;
789                 ip->i_inode.i_ctime = CURRENT_TIME;
790                 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
791                 gfs2_dinode_out(ip, dibh->b_data);
792                 brelse(dibh);
793         }
794
795         error = gfs2_dir_del(odip, &odentry->d_name);
796         if (error)
797                 goto out_end_trans;
798
799         error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
800         if (error)
801                 goto out_end_trans;
802
803 out_end_trans:
804         gfs2_trans_end(sdp);
805 out_ipreserv:
806         if (alloc_required)
807                 gfs2_inplace_release(ndip);
808 out_gunlock_q:
809         if (alloc_required)
810                 gfs2_quota_unlock(ndip);
811 out_alloc:
812         if (alloc_required)
813                 gfs2_alloc_put(ndip);
814 out_gunlock:
815         gfs2_glock_dq_m(num_gh, ghs);
816 out_uninit:
817         for (x = 0; x < num_gh; x++)
818                 gfs2_holder_uninit(ghs + x);
819 out_gunlock_r:
820         if (dir_rename)
821                 gfs2_glock_dq_uninit(&r_gh);
822 out:
823         return error;
824 }
825
826 /**
827  * gfs2_readlink - Read the value of a symlink
828  * @dentry: the symlink
829  * @buf: the buffer to read the symlink data into
830  * @size: the size of the buffer
831  *
832  * Returns: errno
833  */
834
835 static int gfs2_readlink(struct dentry *dentry, char __user *user_buf,
836                          int user_size)
837 {
838         struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
839         char array[GFS2_FAST_NAME_SIZE], *buf = array;
840         unsigned int len = GFS2_FAST_NAME_SIZE;
841         int error;
842
843         error = gfs2_readlinki(ip, &buf, &len);
844         if (error)
845                 return error;
846
847         if (user_size > len - 1)
848                 user_size = len - 1;
849
850         if (copy_to_user(user_buf, buf, user_size))
851                 error = -EFAULT;
852         else
853                 error = user_size;
854
855         if (buf != array)
856                 kfree(buf);
857
858         return error;
859 }
860
861 /**
862  * gfs2_follow_link - Follow a symbolic link
863  * @dentry: The dentry of the link
864  * @nd: Data that we pass to vfs_follow_link()
865  *
866  * This can handle symlinks of any size. It is optimised for symlinks
867  * under GFS2_FAST_NAME_SIZE.
868  *
869  * Returns: 0 on success or error code
870  */
871
872 static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
873 {
874         struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
875         char array[GFS2_FAST_NAME_SIZE], *buf = array;
876         unsigned int len = GFS2_FAST_NAME_SIZE;
877         int error;
878
879         error = gfs2_readlinki(ip, &buf, &len);
880         if (!error) {
881                 error = vfs_follow_link(nd, buf);
882                 if (buf != array)
883                         kfree(buf);
884         }
885
886         return ERR_PTR(error);
887 }
888
889 /**
890  * gfs2_permission -
891  * @inode:
892  * @mask:
893  * @nd: passed from Linux VFS, ignored by us
894  *
895  * This may be called from the VFS directly, or from within GFS2 with the
896  * inode locked, so we look to see if the glock is already locked and only
897  * lock the glock if its not already been done.
898  *
899  * Returns: errno
900  */
901
902 static int gfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
903 {
904         struct gfs2_inode *ip = GFS2_I(inode);
905         struct gfs2_holder i_gh;
906         int error;
907         int unlock = 0;
908
909         if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
910                 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
911                 if (error)
912                         return error;
913                 unlock = 1;
914         }
915
916         error = generic_permission(inode, mask, gfs2_check_acl);
917         if (unlock)
918                 gfs2_glock_dq_uninit(&i_gh);
919
920         return error;
921 }
922
923 static int setattr_size(struct inode *inode, struct iattr *attr)
924 {
925         struct gfs2_inode *ip = GFS2_I(inode);
926         struct gfs2_sbd *sdp = GFS2_SB(inode);
927         int error;
928
929         if (attr->ia_size != ip->i_di.di_size) {
930                 error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
931                 if (error)
932                         return error;
933                 error = vmtruncate(inode, attr->ia_size);
934                 gfs2_trans_end(sdp);
935                 if (error) 
936                         return error;
937         }
938
939         error = gfs2_truncatei(ip, attr->ia_size);
940         if (error && (inode->i_size != ip->i_di.di_size))
941                 i_size_write(inode, ip->i_di.di_size);
942
943         return error;
944 }
945
946 static int setattr_chown(struct inode *inode, struct iattr *attr)
947 {
948         struct gfs2_inode *ip = GFS2_I(inode);
949         struct gfs2_sbd *sdp = GFS2_SB(inode);
950         struct buffer_head *dibh;
951         u32 ouid, ogid, nuid, ngid;
952         int error;
953
954         ouid = inode->i_uid;
955         ogid = inode->i_gid;
956         nuid = attr->ia_uid;
957         ngid = attr->ia_gid;
958
959         if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
960                 ouid = nuid = NO_QUOTA_CHANGE;
961         if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
962                 ogid = ngid = NO_QUOTA_CHANGE;
963
964         if (!gfs2_alloc_get(ip))
965                 return -ENOMEM;
966
967         error = gfs2_quota_lock(ip, nuid, ngid);
968         if (error)
969                 goto out_alloc;
970
971         if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
972                 error = gfs2_quota_check(ip, nuid, ngid);
973                 if (error)
974                         goto out_gunlock_q;
975         }
976
977         error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
978         if (error)
979                 goto out_gunlock_q;
980
981         error = gfs2_meta_inode_buffer(ip, &dibh);
982         if (error)
983                 goto out_end_trans;
984
985         error = inode_setattr(inode, attr);
986         gfs2_assert_warn(sdp, !error);
987
988         gfs2_trans_add_bh(ip->i_gl, dibh, 1);
989         gfs2_dinode_out(ip, dibh->b_data);
990         brelse(dibh);
991
992         if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
993                 u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
994                 gfs2_quota_change(ip, -blocks, ouid, ogid);
995                 gfs2_quota_change(ip, blocks, nuid, ngid);
996         }
997
998 out_end_trans:
999         gfs2_trans_end(sdp);
1000 out_gunlock_q:
1001         gfs2_quota_unlock(ip);
1002 out_alloc:
1003         gfs2_alloc_put(ip);
1004         return error;
1005 }
1006
1007 /**
1008  * gfs2_setattr - Change attributes on an inode
1009  * @dentry: The dentry which is changing
1010  * @attr: The structure describing the change
1011  *
1012  * The VFS layer wants to change one or more of an inodes attributes.  Write
1013  * that change out to disk.
1014  *
1015  * Returns: errno
1016  */
1017
1018 static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
1019 {
1020         struct inode *inode = dentry->d_inode;
1021         struct gfs2_inode *ip = GFS2_I(inode);
1022         struct gfs2_holder i_gh;
1023         int error;
1024
1025         error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1026         if (error)
1027                 return error;
1028
1029         error = -EPERM;
1030         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1031                 goto out;
1032
1033         error = inode_change_ok(inode, attr);
1034         if (error)
1035                 goto out;
1036
1037         if (attr->ia_valid & ATTR_SIZE)
1038                 error = setattr_size(inode, attr);
1039         else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1040                 error = setattr_chown(inode, attr);
1041         else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1042                 error = gfs2_acl_chmod(ip, attr);
1043         else
1044                 error = gfs2_setattr_simple(ip, attr);
1045
1046 out:
1047         gfs2_glock_dq_uninit(&i_gh);
1048         if (!error)
1049                 mark_inode_dirty(inode);
1050         return error;
1051 }
1052
1053 /**
1054  * gfs2_getattr - Read out an inode's attributes
1055  * @mnt: The vfsmount the inode is being accessed from
1056  * @dentry: The dentry to stat
1057  * @stat: The inode's stats
1058  *
1059  * This may be called from the VFS directly, or from within GFS2 with the
1060  * inode locked, so we look to see if the glock is already locked and only
1061  * lock the glock if its not already been done. Note that its the NFS
1062  * readdirplus operation which causes this to be called (from filldir)
1063  * with the glock already held.
1064  *
1065  * Returns: errno
1066  */
1067
1068 static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1069                         struct kstat *stat)
1070 {
1071         struct inode *inode = dentry->d_inode;
1072         struct gfs2_inode *ip = GFS2_I(inode);
1073         struct gfs2_holder gh;
1074         int error;
1075         int unlock = 0;
1076
1077         if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
1078                 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1079                 if (error)
1080                         return error;
1081                 unlock = 1;
1082         }
1083
1084         generic_fillattr(inode, stat);
1085         if (unlock)
1086                 gfs2_glock_dq_uninit(&gh);
1087
1088         return 0;
1089 }
1090
1091 static int gfs2_setxattr(struct dentry *dentry, const char *name,
1092                          const void *data, size_t size, int flags)
1093 {
1094         struct inode *inode = dentry->d_inode;
1095         struct gfs2_ea_request er;
1096
1097         memset(&er, 0, sizeof(struct gfs2_ea_request));
1098         er.er_type = gfs2_ea_name2type(name, &er.er_name);
1099         if (er.er_type == GFS2_EATYPE_UNUSED)
1100                 return -EOPNOTSUPP;
1101         er.er_data = (char *)data;
1102         er.er_name_len = strlen(er.er_name);
1103         er.er_data_len = size;
1104         er.er_flags = flags;
1105
1106         gfs2_assert_warn(GFS2_SB(inode), !(er.er_flags & GFS2_ERF_MODE));
1107
1108         return gfs2_ea_set(GFS2_I(inode), &er);
1109 }
1110
1111 static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1112                              void *data, size_t size)
1113 {
1114         struct gfs2_ea_request er;
1115
1116         memset(&er, 0, sizeof(struct gfs2_ea_request));
1117         er.er_type = gfs2_ea_name2type(name, &er.er_name);
1118         if (er.er_type == GFS2_EATYPE_UNUSED)
1119                 return -EOPNOTSUPP;
1120         er.er_data = data;
1121         er.er_name_len = strlen(er.er_name);
1122         er.er_data_len = size;
1123
1124         return gfs2_ea_get(GFS2_I(dentry->d_inode), &er);
1125 }
1126
1127 static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
1128 {
1129         struct gfs2_ea_request er;
1130
1131         memset(&er, 0, sizeof(struct gfs2_ea_request));
1132         er.er_data = (size) ? buffer : NULL;
1133         er.er_data_len = size;
1134
1135         return gfs2_ea_list(GFS2_I(dentry->d_inode), &er);
1136 }
1137
1138 static int gfs2_removexattr(struct dentry *dentry, const char *name)
1139 {
1140         struct gfs2_ea_request er;
1141
1142         memset(&er, 0, sizeof(struct gfs2_ea_request));
1143         er.er_type = gfs2_ea_name2type(name, &er.er_name);
1144         if (er.er_type == GFS2_EATYPE_UNUSED)
1145                 return -EOPNOTSUPP;
1146         er.er_name_len = strlen(er.er_name);
1147
1148         return gfs2_ea_remove(GFS2_I(dentry->d_inode), &er);
1149 }
1150
1151 const struct inode_operations gfs2_file_iops = {
1152         .permission = gfs2_permission,
1153         .setattr = gfs2_setattr,
1154         .getattr = gfs2_getattr,
1155         .setxattr = gfs2_setxattr,
1156         .getxattr = gfs2_getxattr,
1157         .listxattr = gfs2_listxattr,
1158         .removexattr = gfs2_removexattr,
1159 };
1160
1161 const struct inode_operations gfs2_dir_iops = {
1162         .create = gfs2_create,
1163         .lookup = gfs2_lookup,
1164         .link = gfs2_link,
1165         .unlink = gfs2_unlink,
1166         .symlink = gfs2_symlink,
1167         .mkdir = gfs2_mkdir,
1168         .rmdir = gfs2_rmdir,
1169         .mknod = gfs2_mknod,
1170         .rename = gfs2_rename,
1171         .permission = gfs2_permission,
1172         .setattr = gfs2_setattr,
1173         .getattr = gfs2_getattr,
1174         .setxattr = gfs2_setxattr,
1175         .getxattr = gfs2_getxattr,
1176         .listxattr = gfs2_listxattr,
1177         .removexattr = gfs2_removexattr,
1178 };
1179
1180 const struct inode_operations gfs2_symlink_iops = {
1181         .readlink = gfs2_readlink,
1182         .follow_link = gfs2_follow_link,
1183         .permission = gfs2_permission,
1184         .setattr = gfs2_setattr,
1185         .getattr = gfs2_getattr,
1186         .setxattr = gfs2_setxattr,
1187         .getxattr = gfs2_getxattr,
1188         .listxattr = gfs2_listxattr,
1189         .removexattr = gfs2_removexattr,
1190 };
1191