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