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