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