GFS2: fix d_splice_alias() misuses
[platform/kernel/linux-stable.git] / fs / gfs2 / inode.c
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2011 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/mm.h>
16 #include <linux/xattr.h>
17 #include <linux/posix_acl.h>
18 #include <linux/gfs2_ondisk.h>
19 #include <linux/crc32.h>
20 #include <linux/fiemap.h>
21 #include <linux/security.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 "xattr.h"
30 #include "glock.h"
31 #include "inode.h"
32 #include "meta_io.h"
33 #include "quota.h"
34 #include "rgrp.h"
35 #include "trans.h"
36 #include "util.h"
37 #include "super.h"
38 #include "glops.h"
39
40 struct gfs2_skip_data {
41         u64 no_addr;
42         int skipped;
43         int non_block;
44 };
45
46 static int iget_test(struct inode *inode, void *opaque)
47 {
48         struct gfs2_inode *ip = GFS2_I(inode);
49         struct gfs2_skip_data *data = opaque;
50
51         if (ip->i_no_addr == data->no_addr) {
52                 if (data->non_block &&
53                     inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)) {
54                         data->skipped = 1;
55                         return 0;
56                 }
57                 return 1;
58         }
59         return 0;
60 }
61
62 static int iget_set(struct inode *inode, void *opaque)
63 {
64         struct gfs2_inode *ip = GFS2_I(inode);
65         struct gfs2_skip_data *data = opaque;
66
67         if (data->skipped)
68                 return -ENOENT;
69         inode->i_ino = (unsigned long)(data->no_addr);
70         ip->i_no_addr = data->no_addr;
71         return 0;
72 }
73
74 struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr, int non_block)
75 {
76         unsigned long hash = (unsigned long)no_addr;
77         struct gfs2_skip_data data;
78
79         data.no_addr = no_addr;
80         data.skipped = 0;
81         data.non_block = non_block;
82         return ilookup5(sb, hash, iget_test, &data);
83 }
84
85 static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr,
86                                int non_block)
87 {
88         struct gfs2_skip_data data;
89         unsigned long hash = (unsigned long)no_addr;
90
91         data.no_addr = no_addr;
92         data.skipped = 0;
93         data.non_block = non_block;
94         return iget5_locked(sb, hash, iget_test, iget_set, &data);
95 }
96
97 /**
98  * gfs2_set_iop - Sets inode operations
99  * @inode: The inode with correct i_mode filled in
100  *
101  * GFS2 lookup code fills in vfs inode contents based on info obtained
102  * from directory entry inside gfs2_inode_lookup().
103  */
104
105 static void gfs2_set_iop(struct inode *inode)
106 {
107         struct gfs2_sbd *sdp = GFS2_SB(inode);
108         umode_t mode = inode->i_mode;
109
110         if (S_ISREG(mode)) {
111                 inode->i_op = &gfs2_file_iops;
112                 if (gfs2_localflocks(sdp))
113                         inode->i_fop = &gfs2_file_fops_nolock;
114                 else
115                         inode->i_fop = &gfs2_file_fops;
116         } else if (S_ISDIR(mode)) {
117                 inode->i_op = &gfs2_dir_iops;
118                 if (gfs2_localflocks(sdp))
119                         inode->i_fop = &gfs2_dir_fops_nolock;
120                 else
121                         inode->i_fop = &gfs2_dir_fops;
122         } else if (S_ISLNK(mode)) {
123                 inode->i_op = &gfs2_symlink_iops;
124         } else {
125                 inode->i_op = &gfs2_file_iops;
126                 init_special_inode(inode, inode->i_mode, inode->i_rdev);
127         }
128 }
129
130 /**
131  * gfs2_inode_lookup - Lookup an inode
132  * @sb: The super block
133  * @no_addr: The inode number
134  * @type: The type of the inode
135  * non_block: Can we block on inodes that are being freed?
136  *
137  * Returns: A VFS inode, or an error
138  */
139
140 struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
141                                 u64 no_addr, u64 no_formal_ino, int non_block)
142 {
143         struct inode *inode;
144         struct gfs2_inode *ip;
145         struct gfs2_glock *io_gl = NULL;
146         int error;
147
148         inode = gfs2_iget(sb, no_addr, non_block);
149         ip = GFS2_I(inode);
150
151         if (!inode)
152                 return ERR_PTR(-ENOMEM);
153
154         if (inode->i_state & I_NEW) {
155                 struct gfs2_sbd *sdp = GFS2_SB(inode);
156                 ip->i_no_formal_ino = no_formal_ino;
157
158                 error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
159                 if (unlikely(error))
160                         goto fail;
161                 ip->i_gl->gl_object = ip;
162
163                 error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
164                 if (unlikely(error))
165                         goto fail_put;
166
167                 set_bit(GIF_INVALID, &ip->i_flags);
168                 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
169                 if (unlikely(error))
170                         goto fail_iopen;
171
172                 ip->i_iopen_gh.gh_gl->gl_object = ip;
173                 gfs2_glock_put(io_gl);
174                 io_gl = NULL;
175
176                 if (type == DT_UNKNOWN) {
177                         /* Inode glock must be locked already */
178                         error = gfs2_inode_refresh(GFS2_I(inode));
179                         if (error)
180                                 goto fail_refresh;
181                 } else {
182                         inode->i_mode = DT2IF(type);
183                 }
184
185                 gfs2_set_iop(inode);
186                 unlock_new_inode(inode);
187         }
188
189         return inode;
190
191 fail_refresh:
192         ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
193         ip->i_iopen_gh.gh_gl->gl_object = NULL;
194         gfs2_glock_dq_uninit(&ip->i_iopen_gh);
195 fail_iopen:
196         if (io_gl)
197                 gfs2_glock_put(io_gl);
198 fail_put:
199         ip->i_gl->gl_object = NULL;
200         gfs2_glock_put(ip->i_gl);
201 fail:
202         iget_failed(inode);
203         return ERR_PTR(error);
204 }
205
206 struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
207                                   u64 *no_formal_ino, unsigned int blktype)
208 {
209         struct super_block *sb = sdp->sd_vfs;
210         struct gfs2_holder i_gh;
211         struct inode *inode = NULL;
212         int error;
213
214         /* Must not read in block until block type is verified */
215         error = gfs2_glock_nq_num(sdp, no_addr, &gfs2_inode_glops,
216                                   LM_ST_EXCLUSIVE, GL_SKIP, &i_gh);
217         if (error)
218                 return ERR_PTR(error);
219
220         error = gfs2_check_blk_type(sdp, no_addr, blktype);
221         if (error)
222                 goto fail;
223
224         inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, 0, 1);
225         if (IS_ERR(inode))
226                 goto fail;
227
228         /* Two extra checks for NFS only */
229         if (no_formal_ino) {
230                 error = -ESTALE;
231                 if (GFS2_I(inode)->i_no_formal_ino != *no_formal_ino)
232                         goto fail_iput;
233
234                 error = -EIO;
235                 if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
236                         goto fail_iput;
237
238                 error = 0;
239         }
240
241 fail:
242         gfs2_glock_dq_uninit(&i_gh);
243         return error ? ERR_PTR(error) : inode;
244 fail_iput:
245         iput(inode);
246         goto fail;
247 }
248
249
250 struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
251 {
252         struct qstr qstr;
253         struct inode *inode;
254         gfs2_str2qstr(&qstr, name);
255         inode = gfs2_lookupi(dip, &qstr, 1);
256         /* gfs2_lookupi has inconsistent callers: vfs
257          * related routines expect NULL for no entry found,
258          * gfs2_lookup_simple callers expect ENOENT
259          * and do not check for NULL.
260          */
261         if (inode == NULL)
262                 return ERR_PTR(-ENOENT);
263         else
264                 return inode;
265 }
266
267
268 /**
269  * gfs2_lookupi - Look up a filename in a directory and return its inode
270  * @d_gh: An initialized holder for the directory glock
271  * @name: The name of the inode to look for
272  * @is_root: If 1, ignore the caller's permissions
273  * @i_gh: An uninitialized holder for the new inode glock
274  *
275  * This can be called via the VFS filldir function when NFS is doing
276  * a readdirplus and the inode which its intending to stat isn't
277  * already in cache. In this case we must not take the directory glock
278  * again, since the readdir call will have already taken that lock.
279  *
280  * Returns: errno
281  */
282
283 struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
284                            int is_root)
285 {
286         struct super_block *sb = dir->i_sb;
287         struct gfs2_inode *dip = GFS2_I(dir);
288         struct gfs2_holder d_gh;
289         int error = 0;
290         struct inode *inode = NULL;
291         int unlock = 0;
292
293         if (!name->len || name->len > GFS2_FNAMESIZE)
294                 return ERR_PTR(-ENAMETOOLONG);
295
296         if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
297             (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
298              dir == sb->s_root->d_inode)) {
299                 igrab(dir);
300                 return dir;
301         }
302
303         if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
304                 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
305                 if (error)
306                         return ERR_PTR(error);
307                 unlock = 1;
308         }
309
310         if (!is_root) {
311                 error = gfs2_permission(dir, MAY_EXEC);
312                 if (error)
313                         goto out;
314         }
315
316         inode = gfs2_dir_search(dir, name, false);
317         if (IS_ERR(inode))
318                 error = PTR_ERR(inode);
319 out:
320         if (unlock)
321                 gfs2_glock_dq_uninit(&d_gh);
322         if (error == -ENOENT)
323                 return NULL;
324         return inode ? inode : ERR_PTR(error);
325 }
326
327 /**
328  * create_ok - OK to create a new on-disk inode here?
329  * @dip:  Directory in which dinode is to be created
330  * @name:  Name of new dinode
331  * @mode:
332  *
333  * Returns: errno
334  */
335
336 static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
337                      umode_t mode)
338 {
339         int error;
340
341         error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
342         if (error)
343                 return error;
344
345         /*  Don't create entries in an unlinked directory  */
346         if (!dip->i_inode.i_nlink)
347                 return -ENOENT;
348
349         if (dip->i_entries == (u32)-1)
350                 return -EFBIG;
351         if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
352                 return -EMLINK;
353
354         return 0;
355 }
356
357 static void munge_mode_uid_gid(const struct gfs2_inode *dip,
358                                struct inode *inode)
359 {
360         if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
361             (dip->i_inode.i_mode & S_ISUID) &&
362             !uid_eq(dip->i_inode.i_uid, GLOBAL_ROOT_UID)) {
363                 if (S_ISDIR(inode->i_mode))
364                         inode->i_mode |= S_ISUID;
365                 else if (!uid_eq(dip->i_inode.i_uid, current_fsuid()))
366                         inode->i_mode &= ~07111;
367                 inode->i_uid = dip->i_inode.i_uid;
368         } else
369                 inode->i_uid = current_fsuid();
370
371         if (dip->i_inode.i_mode & S_ISGID) {
372                 if (S_ISDIR(inode->i_mode))
373                         inode->i_mode |= S_ISGID;
374                 inode->i_gid = dip->i_inode.i_gid;
375         } else
376                 inode->i_gid = current_fsgid();
377 }
378
379 static int alloc_dinode(struct gfs2_inode *ip, u32 flags)
380 {
381         struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
382         struct gfs2_alloc_parms ap = { .target = RES_DINODE, .aflags = flags, };
383         int error;
384         int dblocks = 1;
385
386         error = gfs2_quota_lock_check(ip);
387         if (error)
388                 goto out;
389
390         error = gfs2_inplace_reserve(ip, &ap);
391         if (error)
392                 goto out_quota;
393
394         error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 0);
395         if (error)
396                 goto out_ipreserv;
397
398         error = gfs2_alloc_blocks(ip, &ip->i_no_addr, &dblocks, 1, &ip->i_generation);
399         ip->i_no_formal_ino = ip->i_generation;
400         ip->i_inode.i_ino = ip->i_no_addr;
401         ip->i_goal = ip->i_no_addr;
402
403         gfs2_trans_end(sdp);
404
405 out_ipreserv:
406         gfs2_inplace_release(ip);
407 out_quota:
408         gfs2_quota_unlock(ip);
409 out:
410         return error;
411 }
412
413 static void gfs2_init_dir(struct buffer_head *dibh,
414                           const struct gfs2_inode *parent)
415 {
416         struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
417         struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
418
419         gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent);
420         dent->de_inum = di->di_num; /* already GFS2 endian */
421         dent->de_type = cpu_to_be16(DT_DIR);
422
423         dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
424         gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
425         gfs2_inum_out(parent, dent);
426         dent->de_type = cpu_to_be16(DT_DIR);
427         
428 }
429
430 /**
431  * init_dinode - Fill in a new dinode structure
432  * @dip: The directory this inode is being created in
433  * @ip: The inode
434  * @symname: The symlink destination (if a symlink)
435  * @bhp: The buffer head (returned to caller)
436  *
437  */
438
439 static void init_dinode(struct gfs2_inode *dip, struct gfs2_inode *ip,
440                         const char *symname)
441 {
442         struct gfs2_dinode *di;
443         struct buffer_head *dibh;
444
445         dibh = gfs2_meta_new(ip->i_gl, ip->i_no_addr);
446         gfs2_trans_add_meta(ip->i_gl, dibh);
447         di = (struct gfs2_dinode *)dibh->b_data;
448         gfs2_dinode_out(ip, di);
449
450         di->di_major = cpu_to_be32(MAJOR(ip->i_inode.i_rdev));
451         di->di_minor = cpu_to_be32(MINOR(ip->i_inode.i_rdev));
452         di->__pad1 = 0;
453         di->__pad2 = 0;
454         di->__pad3 = 0;
455         memset(&di->__pad4, 0, sizeof(di->__pad4));
456         memset(&di->di_reserved, 0, sizeof(di->di_reserved));
457         gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
458
459         switch(ip->i_inode.i_mode & S_IFMT) {
460         case S_IFDIR:
461                 gfs2_init_dir(dibh, dip);
462                 break;
463         case S_IFLNK:
464                 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, ip->i_inode.i_size);
465                 break;
466         }
467
468         set_buffer_uptodate(dibh);
469         brelse(dibh);
470 }
471
472 /**
473  * gfs2_trans_da_blocks - Calculate number of blocks to link inode
474  * @dip: The directory we are linking into
475  * @da: The dir add information
476  * @nr_inodes: The number of inodes involved
477  *
478  * This calculate the number of blocks we need to reserve in a
479  * transaction to link @nr_inodes into a directory. In most cases
480  * @nr_inodes will be 2 (the directory plus the inode being linked in)
481  * but in case of rename, 4 may be required.
482  *
483  * Returns: Number of blocks
484  */
485
486 static unsigned gfs2_trans_da_blks(const struct gfs2_inode *dip,
487                                    const struct gfs2_diradd *da,
488                                    unsigned nr_inodes)
489 {
490         return da->nr_blocks + gfs2_rg_blocks(dip, da->nr_blocks) +
491                (nr_inodes * RES_DINODE) + RES_QUOTA + RES_STATFS;
492 }
493
494 static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
495                        struct gfs2_inode *ip, struct gfs2_diradd *da)
496 {
497         struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
498         struct gfs2_alloc_parms ap = { .target = da->nr_blocks, };
499         int error;
500
501         if (da->nr_blocks) {
502                 error = gfs2_quota_lock_check(dip);
503                 if (error)
504                         goto fail_quota_locks;
505
506                 error = gfs2_inplace_reserve(dip, &ap);
507                 if (error)
508                         goto fail_quota_locks;
509
510                 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, da, 2), 0);
511                 if (error)
512                         goto fail_ipreserv;
513         } else {
514                 error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
515                 if (error)
516                         goto fail_quota_locks;
517         }
518
519         error = gfs2_dir_add(&dip->i_inode, name, ip, da);
520         if (error)
521                 goto fail_end_trans;
522
523 fail_end_trans:
524         gfs2_trans_end(sdp);
525 fail_ipreserv:
526         gfs2_inplace_release(dip);
527 fail_quota_locks:
528         gfs2_quota_unlock(dip);
529         return error;
530 }
531
532 static int gfs2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
533                     void *fs_info)
534 {
535         const struct xattr *xattr;
536         int err = 0;
537
538         for (xattr = xattr_array; xattr->name != NULL; xattr++) {
539                 err = __gfs2_xattr_set(inode, xattr->name, xattr->value,
540                                        xattr->value_len, 0,
541                                        GFS2_EATYPE_SECURITY);
542                 if (err < 0)
543                         break;
544         }
545         return err;
546 }
547
548 static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip,
549                               const struct qstr *qstr)
550 {
551         return security_inode_init_security(&ip->i_inode, &dip->i_inode, qstr,
552                                             &gfs2_initxattrs, NULL);
553 }
554
555 /**
556  * gfs2_create_inode - Create a new inode
557  * @dir: The parent directory
558  * @dentry: The new dentry
559  * @file: If non-NULL, the file which is being opened
560  * @mode: The permissions on the new inode
561  * @dev: For device nodes, this is the device number
562  * @symname: For symlinks, this is the link destination
563  * @size: The initial size of the inode (ignored for directories)
564  *
565  * Returns: 0 on success, or error code
566  */
567
568 static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
569                              struct file *file,
570                              umode_t mode, dev_t dev, const char *symname,
571                              unsigned int size, int excl, int *opened)
572 {
573         const struct qstr *name = &dentry->d_name;
574         struct posix_acl *default_acl, *acl;
575         struct gfs2_holder ghs[2];
576         struct inode *inode = NULL;
577         struct gfs2_inode *dip = GFS2_I(dir), *ip;
578         struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
579         struct gfs2_glock *io_gl;
580         struct dentry *d;
581         int error;
582         u32 aflags = 0;
583         struct gfs2_diradd da = { .bh = NULL, };
584
585         if (!name->len || name->len > GFS2_FNAMESIZE)
586                 return -ENAMETOOLONG;
587
588         error = gfs2_rs_alloc(dip);
589         if (error)
590                 return error;
591
592         error = gfs2_rindex_update(sdp);
593         if (error)
594                 return error;
595
596         error = gfs2_glock_nq_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
597         if (error)
598                 goto fail;
599
600         error = create_ok(dip, name, mode);
601         if (error)
602                 goto fail_gunlock;
603
604         inode = gfs2_dir_search(dir, &dentry->d_name, !S_ISREG(mode) || excl);
605         error = PTR_ERR(inode);
606         if (!IS_ERR(inode)) {
607                 d = d_splice_alias(inode, dentry);
608                 error = PTR_ERR(d);
609                 if (IS_ERR(d)) {
610                         inode = ERR_CAST(d);
611                         goto fail_gunlock;
612                 }
613                 error = 0;
614                 if (file) {
615                         if (S_ISREG(inode->i_mode)) {
616                                 WARN_ON(d != NULL);
617                                 error = finish_open(file, dentry, gfs2_open_common, opened);
618                         } else {
619                                 error = finish_no_open(file, d);
620                         }
621                 } else {
622                         dput(d);
623                 }
624                 gfs2_glock_dq_uninit(ghs);
625                 return error;
626         } else if (error != -ENOENT) {
627                 goto fail_gunlock;
628         }
629
630         error = gfs2_diradd_alloc_required(dir, name, &da);
631         if (error < 0)
632                 goto fail_gunlock;
633
634         inode = new_inode(sdp->sd_vfs);
635         error = -ENOMEM;
636         if (!inode)
637                 goto fail_gunlock;
638
639         error = posix_acl_create(dir, &mode, &default_acl, &acl);
640         if (error)
641                 goto fail_free_vfs_inode;
642
643         ip = GFS2_I(inode);
644         error = gfs2_rs_alloc(ip);
645         if (error)
646                 goto fail_free_acls;
647
648         inode->i_mode = mode;
649         set_nlink(inode, S_ISDIR(mode) ? 2 : 1);
650         inode->i_rdev = dev;
651         inode->i_size = size;
652         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
653         gfs2_set_inode_blocks(inode, 1);
654         munge_mode_uid_gid(dip, inode);
655         ip->i_goal = dip->i_goal;
656         ip->i_diskflags = 0;
657         ip->i_eattr = 0;
658         ip->i_height = 0;
659         ip->i_depth = 0;
660         ip->i_entries = 0;
661
662         switch(mode & S_IFMT) {
663         case S_IFREG:
664                 if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
665                     gfs2_tune_get(sdp, gt_new_files_jdata))
666                         ip->i_diskflags |= GFS2_DIF_JDATA;
667                 gfs2_set_aops(inode);
668                 break;
669         case S_IFDIR:
670                 ip->i_diskflags |= (dip->i_diskflags & GFS2_DIF_INHERIT_JDATA);
671                 ip->i_diskflags |= GFS2_DIF_JDATA;
672                 ip->i_entries = 2;
673                 break;
674         }
675         gfs2_set_inode_flags(inode);
676
677         if ((GFS2_I(sdp->sd_root_dir->d_inode) == dip) ||
678             (dip->i_diskflags & GFS2_DIF_TOPDIR))
679                 aflags |= GFS2_AF_ORLOV;
680
681         error = alloc_dinode(ip, aflags);
682         if (error)
683                 goto fail_free_inode;
684
685         error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
686         if (error)
687                 goto fail_free_inode;
688
689         ip->i_gl->gl_object = ip;
690         error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
691         if (error)
692                 goto fail_free_inode;
693
694         error = gfs2_trans_begin(sdp, RES_DINODE, 0);
695         if (error)
696                 goto fail_gunlock2;
697
698         init_dinode(dip, ip, symname);
699         gfs2_trans_end(sdp);
700
701         error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
702         if (error)
703                 goto fail_gunlock2;
704
705         error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
706         if (error)
707                 goto fail_gunlock2;
708
709         ip->i_iopen_gh.gh_gl->gl_object = ip;
710         gfs2_glock_put(io_gl);
711         gfs2_set_iop(inode);
712         insert_inode_hash(inode);
713
714         if (default_acl) {
715                 error = gfs2_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
716                 posix_acl_release(default_acl);
717         }
718         if (acl) {
719                 if (!error)
720                         error = gfs2_set_acl(inode, acl, ACL_TYPE_ACCESS);
721                 posix_acl_release(acl);
722         }
723
724         if (error)
725                 goto fail_gunlock3;
726
727         error = gfs2_security_init(dip, ip, name);
728         if (error)
729                 goto fail_gunlock3;
730
731         error = link_dinode(dip, name, ip, &da);
732         if (error)
733                 goto fail_gunlock3;
734
735         mark_inode_dirty(inode);
736         d_instantiate(dentry, inode);
737         if (file) {
738                 *opened |= FILE_CREATED;
739                 error = finish_open(file, dentry, gfs2_open_common, opened);
740         }
741         gfs2_glock_dq_uninit(ghs);
742         gfs2_glock_dq_uninit(ghs + 1);
743         return error;
744
745 fail_gunlock3:
746         gfs2_glock_dq_uninit(ghs + 1);
747         if (ip->i_gl)
748                 gfs2_glock_put(ip->i_gl);
749         goto fail_gunlock;
750
751 fail_gunlock2:
752         gfs2_glock_dq_uninit(ghs + 1);
753 fail_free_inode:
754         if (ip->i_gl)
755                 gfs2_glock_put(ip->i_gl);
756         gfs2_rs_delete(ip, NULL);
757 fail_free_acls:
758         if (default_acl)
759                 posix_acl_release(default_acl);
760         if (acl)
761                 posix_acl_release(acl);
762 fail_free_vfs_inode:
763         free_inode_nonrcu(inode);
764         inode = NULL;
765 fail_gunlock:
766         gfs2_dir_no_add(&da);
767         gfs2_glock_dq_uninit(ghs);
768         if (inode && !IS_ERR(inode)) {
769                 clear_nlink(inode);
770                 mark_inode_dirty(inode);
771                 set_bit(GIF_ALLOC_FAILED, &GFS2_I(inode)->i_flags);
772                 iput(inode);
773         }
774 fail:
775         return error;
776 }
777
778 /**
779  * gfs2_create - Create a file
780  * @dir: The directory in which to create the file
781  * @dentry: The dentry of the new file
782  * @mode: The mode of the new file
783  *
784  * Returns: errno
785  */
786
787 static int gfs2_create(struct inode *dir, struct dentry *dentry,
788                        umode_t mode, bool excl)
789 {
790         return gfs2_create_inode(dir, dentry, NULL, S_IFREG | mode, 0, NULL, 0, excl, NULL);
791 }
792
793 /**
794  * __gfs2_lookup - Look up a filename in a directory and return its inode
795  * @dir: The directory inode
796  * @dentry: The dentry of the new inode
797  * @file: File to be opened
798  * @opened: atomic_open flags
799  *
800  *
801  * Returns: errno
802  */
803
804 static struct dentry *__gfs2_lookup(struct inode *dir, struct dentry *dentry,
805                                     struct file *file, int *opened)
806 {
807         struct inode *inode;
808         struct dentry *d;
809         struct gfs2_holder gh;
810         struct gfs2_glock *gl;
811         int error;
812
813         inode = gfs2_lookupi(dir, &dentry->d_name, 0);
814         if (!inode)
815                 return NULL;
816         if (IS_ERR(inode))
817                 return ERR_CAST(inode);
818
819         gl = GFS2_I(inode)->i_gl;
820         error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
821         if (error) {
822                 iput(inode);
823                 return ERR_PTR(error);
824         }
825
826         d = d_splice_alias(inode, dentry);
827         if (IS_ERR(d)) {
828                 gfs2_glock_dq_uninit(&gh);
829                 return d;
830         }
831         if (file && S_ISREG(inode->i_mode))
832                 error = finish_open(file, dentry, gfs2_open_common, opened);
833
834         gfs2_glock_dq_uninit(&gh);
835         if (error) {
836                 dput(d);
837                 return ERR_PTR(error);
838         }
839         return d;
840 }
841
842 static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
843                                   unsigned flags)
844 {
845         return __gfs2_lookup(dir, dentry, NULL, NULL);
846 }
847
848 /**
849  * gfs2_link - Link to a file
850  * @old_dentry: The inode to link
851  * @dir: Add link to this directory
852  * @dentry: The name of the link
853  *
854  * Link the inode in "old_dentry" into the directory "dir" with the
855  * name in "dentry".
856  *
857  * Returns: errno
858  */
859
860 static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
861                      struct dentry *dentry)
862 {
863         struct gfs2_inode *dip = GFS2_I(dir);
864         struct gfs2_sbd *sdp = GFS2_SB(dir);
865         struct inode *inode = old_dentry->d_inode;
866         struct gfs2_inode *ip = GFS2_I(inode);
867         struct gfs2_holder ghs[2];
868         struct buffer_head *dibh;
869         struct gfs2_diradd da = { .bh = NULL, };
870         int error;
871
872         if (S_ISDIR(inode->i_mode))
873                 return -EPERM;
874
875         error = gfs2_rs_alloc(dip);
876         if (error)
877                 return error;
878
879         gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
880         gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
881
882         error = gfs2_glock_nq(ghs); /* parent */
883         if (error)
884                 goto out_parent;
885
886         error = gfs2_glock_nq(ghs + 1); /* child */
887         if (error)
888                 goto out_child;
889
890         error = -ENOENT;
891         if (inode->i_nlink == 0)
892                 goto out_gunlock;
893
894         error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
895         if (error)
896                 goto out_gunlock;
897
898         error = gfs2_dir_check(dir, &dentry->d_name, NULL);
899         switch (error) {
900         case -ENOENT:
901                 break;
902         case 0:
903                 error = -EEXIST;
904         default:
905                 goto out_gunlock;
906         }
907
908         error = -EINVAL;
909         if (!dip->i_inode.i_nlink)
910                 goto out_gunlock;
911         error = -EFBIG;
912         if (dip->i_entries == (u32)-1)
913                 goto out_gunlock;
914         error = -EPERM;
915         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
916                 goto out_gunlock;
917         error = -EINVAL;
918         if (!ip->i_inode.i_nlink)
919                 goto out_gunlock;
920         error = -EMLINK;
921         if (ip->i_inode.i_nlink == (u32)-1)
922                 goto out_gunlock;
923
924         error = gfs2_diradd_alloc_required(dir, &dentry->d_name, &da);
925         if (error < 0)
926                 goto out_gunlock;
927
928         if (da.nr_blocks) {
929                 struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
930                 error = gfs2_quota_lock_check(dip);
931                 if (error)
932                         goto out_gunlock;
933
934                 error = gfs2_inplace_reserve(dip, &ap);
935                 if (error)
936                         goto out_gunlock_q;
937
938                 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, &da, 2), 0);
939                 if (error)
940                         goto out_ipres;
941         } else {
942                 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
943                 if (error)
944                         goto out_ipres;
945         }
946
947         error = gfs2_meta_inode_buffer(ip, &dibh);
948         if (error)
949                 goto out_end_trans;
950
951         error = gfs2_dir_add(dir, &dentry->d_name, ip, &da);
952         if (error)
953                 goto out_brelse;
954
955         gfs2_trans_add_meta(ip->i_gl, dibh);
956         inc_nlink(&ip->i_inode);
957         ip->i_inode.i_ctime = CURRENT_TIME;
958         ihold(inode);
959         d_instantiate(dentry, inode);
960         mark_inode_dirty(inode);
961
962 out_brelse:
963         brelse(dibh);
964 out_end_trans:
965         gfs2_trans_end(sdp);
966 out_ipres:
967         if (da.nr_blocks)
968                 gfs2_inplace_release(dip);
969 out_gunlock_q:
970         if (da.nr_blocks)
971                 gfs2_quota_unlock(dip);
972 out_gunlock:
973         gfs2_dir_no_add(&da);
974         gfs2_glock_dq(ghs + 1);
975 out_child:
976         gfs2_glock_dq(ghs);
977 out_parent:
978         gfs2_holder_uninit(ghs);
979         gfs2_holder_uninit(ghs + 1);
980         return error;
981 }
982
983 /*
984  * gfs2_unlink_ok - check to see that a inode is still in a directory
985  * @dip: the directory
986  * @name: the name of the file
987  * @ip: the inode
988  *
989  * Assumes that the lock on (at least) @dip is held.
990  *
991  * Returns: 0 if the parent/child relationship is correct, errno if it isn't
992  */
993
994 static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
995                           const struct gfs2_inode *ip)
996 {
997         int error;
998
999         if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
1000                 return -EPERM;
1001
1002         if ((dip->i_inode.i_mode & S_ISVTX) &&
1003             !uid_eq(dip->i_inode.i_uid, current_fsuid()) &&
1004             !uid_eq(ip->i_inode.i_uid, current_fsuid()) && !capable(CAP_FOWNER))
1005                 return -EPERM;
1006
1007         if (IS_APPEND(&dip->i_inode))
1008                 return -EPERM;
1009
1010         error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
1011         if (error)
1012                 return error;
1013
1014         error = gfs2_dir_check(&dip->i_inode, name, ip);
1015         if (error)
1016                 return error;
1017
1018         return 0;
1019 }
1020
1021 /**
1022  * gfs2_unlink_inode - Removes an inode from its parent dir and unlinks it
1023  * @dip: The parent directory
1024  * @name: The name of the entry in the parent directory
1025  * @inode: The inode to be removed
1026  *
1027  * Called with all the locks and in a transaction. This will only be
1028  * called for a directory after it has been checked to ensure it is empty.
1029  *
1030  * Returns: 0 on success, or an error
1031  */
1032
1033 static int gfs2_unlink_inode(struct gfs2_inode *dip,
1034                              const struct dentry *dentry)
1035 {
1036         struct inode *inode = dentry->d_inode;
1037         struct gfs2_inode *ip = GFS2_I(inode);
1038         int error;
1039
1040         error = gfs2_dir_del(dip, dentry);
1041         if (error)
1042                 return error;
1043
1044         ip->i_entries = 0;
1045         inode->i_ctime = CURRENT_TIME;
1046         if (S_ISDIR(inode->i_mode))
1047                 clear_nlink(inode);
1048         else
1049                 drop_nlink(inode);
1050         mark_inode_dirty(inode);
1051         if (inode->i_nlink == 0)
1052                 gfs2_unlink_di(inode);
1053         return 0;
1054 }
1055
1056
1057 /**
1058  * gfs2_unlink - Unlink an inode (this does rmdir as well)
1059  * @dir: The inode of the directory containing the inode to unlink
1060  * @dentry: The file itself
1061  *
1062  * This routine uses the type of the inode as a flag to figure out
1063  * whether this is an unlink or an rmdir.
1064  *
1065  * Returns: errno
1066  */
1067
1068 static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
1069 {
1070         struct gfs2_inode *dip = GFS2_I(dir);
1071         struct gfs2_sbd *sdp = GFS2_SB(dir);
1072         struct inode *inode = dentry->d_inode;
1073         struct gfs2_inode *ip = GFS2_I(inode);
1074         struct gfs2_holder ghs[3];
1075         struct gfs2_rgrpd *rgd;
1076         int error;
1077
1078         error = gfs2_rindex_update(sdp);
1079         if (error)
1080                 return error;
1081
1082         error = -EROFS;
1083
1084         gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
1085         gfs2_holder_init(ip->i_gl,  LM_ST_EXCLUSIVE, 0, ghs + 1);
1086
1087         rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1);
1088         if (!rgd)
1089                 goto out_inodes;
1090
1091         gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
1092
1093
1094         error = gfs2_glock_nq(ghs); /* parent */
1095         if (error)
1096                 goto out_parent;
1097
1098         error = gfs2_glock_nq(ghs + 1); /* child */
1099         if (error)
1100                 goto out_child;
1101
1102         error = -ENOENT;
1103         if (inode->i_nlink == 0)
1104                 goto out_rgrp;
1105
1106         if (S_ISDIR(inode->i_mode)) {
1107                 error = -ENOTEMPTY;
1108                 if (ip->i_entries > 2 || inode->i_nlink > 2)
1109                         goto out_rgrp;
1110         }
1111
1112         error = gfs2_glock_nq(ghs + 2); /* rgrp */
1113         if (error)
1114                 goto out_rgrp;
1115
1116         error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
1117         if (error)
1118                 goto out_gunlock;
1119
1120         error = gfs2_trans_begin(sdp, 2*RES_DINODE + 3*RES_LEAF + RES_RG_BIT, 0);
1121         if (error)
1122                 goto out_end_trans;
1123
1124         error = gfs2_unlink_inode(dip, dentry);
1125
1126 out_end_trans:
1127         gfs2_trans_end(sdp);
1128 out_gunlock:
1129         gfs2_glock_dq(ghs + 2);
1130 out_rgrp:
1131         gfs2_glock_dq(ghs + 1);
1132 out_child:
1133         gfs2_glock_dq(ghs);
1134 out_parent:
1135         gfs2_holder_uninit(ghs + 2);
1136 out_inodes:
1137         gfs2_holder_uninit(ghs + 1);
1138         gfs2_holder_uninit(ghs);
1139         return error;
1140 }
1141
1142 /**
1143  * gfs2_symlink - Create a symlink
1144  * @dir: The directory to create the symlink in
1145  * @dentry: The dentry to put the symlink in
1146  * @symname: The thing which the link points to
1147  *
1148  * Returns: errno
1149  */
1150
1151 static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
1152                         const char *symname)
1153 {
1154         struct gfs2_sbd *sdp = GFS2_SB(dir);
1155         unsigned int size;
1156
1157         size = strlen(symname);
1158         if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
1159                 return -ENAMETOOLONG;
1160
1161         return gfs2_create_inode(dir, dentry, NULL, S_IFLNK | S_IRWXUGO, 0, symname, size, 0, NULL);
1162 }
1163
1164 /**
1165  * gfs2_mkdir - Make a directory
1166  * @dir: The parent directory of the new one
1167  * @dentry: The dentry of the new directory
1168  * @mode: The mode of the new directory
1169  *
1170  * Returns: errno
1171  */
1172
1173 static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1174 {
1175         struct gfs2_sbd *sdp = GFS2_SB(dir);
1176         unsigned dsize = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
1177         return gfs2_create_inode(dir, dentry, NULL, S_IFDIR | mode, 0, NULL, dsize, 0, NULL);
1178 }
1179
1180 /**
1181  * gfs2_mknod - Make a special file
1182  * @dir: The directory in which the special file will reside
1183  * @dentry: The dentry of the special file
1184  * @mode: The mode of the special file
1185  * @dev: The device specification of the special file
1186  *
1187  */
1188
1189 static int gfs2_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
1190                       dev_t dev)
1191 {
1192         return gfs2_create_inode(dir, dentry, NULL, mode, dev, NULL, 0, 0, NULL);
1193 }
1194
1195 /**
1196  * gfs2_atomic_open - Atomically open a file
1197  * @dir: The directory
1198  * @dentry: The proposed new entry
1199  * @file: The proposed new struct file
1200  * @flags: open flags
1201  * @mode: File mode
1202  * @opened: Flag to say whether the file has been opened or not
1203  *
1204  * Returns: error code or 0 for success
1205  */
1206
1207 static int gfs2_atomic_open(struct inode *dir, struct dentry *dentry,
1208                             struct file *file, unsigned flags,
1209                             umode_t mode, int *opened)
1210 {
1211         struct dentry *d;
1212         bool excl = !!(flags & O_EXCL);
1213
1214         d = __gfs2_lookup(dir, dentry, file, opened);
1215         if (IS_ERR(d))
1216                 return PTR_ERR(d);
1217         if (d != NULL)
1218                 dentry = d;
1219         if (dentry->d_inode) {
1220                 if (!(*opened & FILE_OPENED)) {
1221                         if (d == NULL)
1222                                 dget(dentry);
1223                         return finish_no_open(file, dentry);
1224                 }
1225                 dput(d);
1226                 return 0;
1227         }
1228
1229         BUG_ON(d != NULL);
1230         if (!(flags & O_CREAT))
1231                 return -ENOENT;
1232
1233         return gfs2_create_inode(dir, dentry, file, S_IFREG | mode, 0, NULL, 0, excl, opened);
1234 }
1235
1236 /*
1237  * gfs2_ok_to_move - check if it's ok to move a directory to another directory
1238  * @this: move this
1239  * @to: to here
1240  *
1241  * Follow @to back to the root and make sure we don't encounter @this
1242  * Assumes we already hold the rename lock.
1243  *
1244  * Returns: errno
1245  */
1246
1247 static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
1248 {
1249         struct inode *dir = &to->i_inode;
1250         struct super_block *sb = dir->i_sb;
1251         struct inode *tmp;
1252         int error = 0;
1253
1254         igrab(dir);
1255
1256         for (;;) {
1257                 if (dir == &this->i_inode) {
1258                         error = -EINVAL;
1259                         break;
1260                 }
1261                 if (dir == sb->s_root->d_inode) {
1262                         error = 0;
1263                         break;
1264                 }
1265
1266                 tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
1267                 if (IS_ERR(tmp)) {
1268                         error = PTR_ERR(tmp);
1269                         break;
1270                 }
1271
1272                 iput(dir);
1273                 dir = tmp;
1274         }
1275
1276         iput(dir);
1277
1278         return error;
1279 }
1280
1281 /**
1282  * gfs2_rename - Rename a file
1283  * @odir: Parent directory of old file name
1284  * @odentry: The old dentry of the file
1285  * @ndir: Parent directory of new file name
1286  * @ndentry: The new dentry of the file
1287  *
1288  * Returns: errno
1289  */
1290
1291 static int gfs2_rename(struct inode *odir, struct dentry *odentry,
1292                        struct inode *ndir, struct dentry *ndentry)
1293 {
1294         struct gfs2_inode *odip = GFS2_I(odir);
1295         struct gfs2_inode *ndip = GFS2_I(ndir);
1296         struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
1297         struct gfs2_inode *nip = NULL;
1298         struct gfs2_sbd *sdp = GFS2_SB(odir);
1299         struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, };
1300         struct gfs2_rgrpd *nrgd;
1301         unsigned int num_gh;
1302         int dir_rename = 0;
1303         struct gfs2_diradd da = { .nr_blocks = 0, };
1304         unsigned int x;
1305         int error;
1306
1307         if (ndentry->d_inode) {
1308                 nip = GFS2_I(ndentry->d_inode);
1309                 if (ip == nip)
1310                         return 0;
1311         }
1312
1313         error = gfs2_rindex_update(sdp);
1314         if (error)
1315                 return error;
1316
1317         error = gfs2_rs_alloc(ndip);
1318         if (error)
1319                 return error;
1320
1321         if (odip != ndip) {
1322                 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
1323                                            0, &r_gh);
1324                 if (error)
1325                         goto out;
1326
1327                 if (S_ISDIR(ip->i_inode.i_mode)) {
1328                         dir_rename = 1;
1329                         /* don't move a dirctory into it's subdir */
1330                         error = gfs2_ok_to_move(ip, ndip);
1331                         if (error)
1332                                 goto out_gunlock_r;
1333                 }
1334         }
1335
1336         num_gh = 1;
1337         gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
1338         if (odip != ndip) {
1339                 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
1340                 num_gh++;
1341         }
1342         gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
1343         num_gh++;
1344
1345         if (nip) {
1346                 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
1347                 num_gh++;
1348                 /* grab the resource lock for unlink flag twiddling 
1349                  * this is the case of the target file already existing
1350                  * so we unlink before doing the rename
1351                  */
1352                 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr, 1);
1353                 if (nrgd)
1354                         gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
1355         }
1356
1357         for (x = 0; x < num_gh; x++) {
1358                 error = gfs2_glock_nq(ghs + x);
1359                 if (error)
1360                         goto out_gunlock;
1361         }
1362
1363         error = -ENOENT;
1364         if (ip->i_inode.i_nlink == 0)
1365                 goto out_gunlock;
1366
1367         /* Check out the old directory */
1368
1369         error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
1370         if (error)
1371                 goto out_gunlock;
1372
1373         /* Check out the new directory */
1374
1375         if (nip) {
1376                 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
1377                 if (error)
1378                         goto out_gunlock;
1379
1380                 if (nip->i_inode.i_nlink == 0) {
1381                         error = -EAGAIN;
1382                         goto out_gunlock;
1383                 }
1384
1385                 if (S_ISDIR(nip->i_inode.i_mode)) {
1386                         if (nip->i_entries < 2) {
1387                                 gfs2_consist_inode(nip);
1388                                 error = -EIO;
1389                                 goto out_gunlock;
1390                         }
1391                         if (nip->i_entries > 2) {
1392                                 error = -ENOTEMPTY;
1393                                 goto out_gunlock;
1394                         }
1395                 }
1396         } else {
1397                 error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
1398                 if (error)
1399                         goto out_gunlock;
1400
1401                 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
1402                 switch (error) {
1403                 case -ENOENT:
1404                         error = 0;
1405                         break;
1406                 case 0:
1407                         error = -EEXIST;
1408                 default:
1409                         goto out_gunlock;
1410                 };
1411
1412                 if (odip != ndip) {
1413                         if (!ndip->i_inode.i_nlink) {
1414                                 error = -ENOENT;
1415                                 goto out_gunlock;
1416                         }
1417                         if (ndip->i_entries == (u32)-1) {
1418                                 error = -EFBIG;
1419                                 goto out_gunlock;
1420                         }
1421                         if (S_ISDIR(ip->i_inode.i_mode) &&
1422                             ndip->i_inode.i_nlink == (u32)-1) {
1423                                 error = -EMLINK;
1424                                 goto out_gunlock;
1425                         }
1426                 }
1427         }
1428
1429         /* Check out the dir to be renamed */
1430
1431         if (dir_rename) {
1432                 error = gfs2_permission(odentry->d_inode, MAY_WRITE);
1433                 if (error)
1434                         goto out_gunlock;
1435         }
1436
1437         if (nip == NULL) {
1438                 error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name, &da);
1439                 if (error)
1440                         goto out_gunlock;
1441         }
1442
1443         if (da.nr_blocks) {
1444                 struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
1445                 error = gfs2_quota_lock_check(ndip);
1446                 if (error)
1447                         goto out_gunlock;
1448
1449                 error = gfs2_inplace_reserve(ndip, &ap);
1450                 if (error)
1451                         goto out_gunlock_q;
1452
1453                 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(ndip, &da, 4) +
1454                                          4 * RES_LEAF + 4, 0);
1455                 if (error)
1456                         goto out_ipreserv;
1457         } else {
1458                 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
1459                                          5 * RES_LEAF + 4, 0);
1460                 if (error)
1461                         goto out_gunlock;
1462         }
1463
1464         /* Remove the target file, if it exists */
1465
1466         if (nip)
1467                 error = gfs2_unlink_inode(ndip, ndentry);
1468
1469         if (dir_rename) {
1470                 error = gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
1471                 if (error)
1472                         goto out_end_trans;
1473         } else {
1474                 struct buffer_head *dibh;
1475                 error = gfs2_meta_inode_buffer(ip, &dibh);
1476                 if (error)
1477                         goto out_end_trans;
1478                 ip->i_inode.i_ctime = CURRENT_TIME;
1479                 gfs2_trans_add_meta(ip->i_gl, dibh);
1480                 gfs2_dinode_out(ip, dibh->b_data);
1481                 brelse(dibh);
1482         }
1483
1484         error = gfs2_dir_del(odip, odentry);
1485         if (error)
1486                 goto out_end_trans;
1487
1488         error = gfs2_dir_add(ndir, &ndentry->d_name, ip, &da);
1489         if (error)
1490                 goto out_end_trans;
1491
1492 out_end_trans:
1493         gfs2_trans_end(sdp);
1494 out_ipreserv:
1495         if (da.nr_blocks)
1496                 gfs2_inplace_release(ndip);
1497 out_gunlock_q:
1498         if (da.nr_blocks)
1499                 gfs2_quota_unlock(ndip);
1500 out_gunlock:
1501         gfs2_dir_no_add(&da);
1502         while (x--) {
1503                 gfs2_glock_dq(ghs + x);
1504                 gfs2_holder_uninit(ghs + x);
1505         }
1506 out_gunlock_r:
1507         if (r_gh.gh_gl)
1508                 gfs2_glock_dq_uninit(&r_gh);
1509 out:
1510         return error;
1511 }
1512
1513 /**
1514  * gfs2_follow_link - Follow a symbolic link
1515  * @dentry: The dentry of the link
1516  * @nd: Data that we pass to vfs_follow_link()
1517  *
1518  * This can handle symlinks of any size.
1519  *
1520  * Returns: 0 on success or error code
1521  */
1522
1523 static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
1524 {
1525         struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
1526         struct gfs2_holder i_gh;
1527         struct buffer_head *dibh;
1528         unsigned int size;
1529         char *buf;
1530         int error;
1531
1532         gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
1533         error = gfs2_glock_nq(&i_gh);
1534         if (error) {
1535                 gfs2_holder_uninit(&i_gh);
1536                 nd_set_link(nd, ERR_PTR(error));
1537                 return NULL;
1538         }
1539
1540         size = (unsigned int)i_size_read(&ip->i_inode);
1541         if (size == 0) {
1542                 gfs2_consist_inode(ip);
1543                 buf = ERR_PTR(-EIO);
1544                 goto out;
1545         }
1546
1547         error = gfs2_meta_inode_buffer(ip, &dibh);
1548         if (error) {
1549                 buf = ERR_PTR(error);
1550                 goto out;
1551         }
1552
1553         buf = kzalloc(size + 1, GFP_NOFS);
1554         if (!buf)
1555                 buf = ERR_PTR(-ENOMEM);
1556         else
1557                 memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
1558         brelse(dibh);
1559 out:
1560         gfs2_glock_dq_uninit(&i_gh);
1561         nd_set_link(nd, buf);
1562         return NULL;
1563 }
1564
1565 /**
1566  * gfs2_permission -
1567  * @inode: The inode
1568  * @mask: The mask to be tested
1569  * @flags: Indicates whether this is an RCU path walk or not
1570  *
1571  * This may be called from the VFS directly, or from within GFS2 with the
1572  * inode locked, so we look to see if the glock is already locked and only
1573  * lock the glock if its not already been done.
1574  *
1575  * Returns: errno
1576  */
1577
1578 int gfs2_permission(struct inode *inode, int mask)
1579 {
1580         struct gfs2_inode *ip;
1581         struct gfs2_holder i_gh;
1582         int error;
1583         int unlock = 0;
1584
1585
1586         ip = GFS2_I(inode);
1587         if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
1588                 if (mask & MAY_NOT_BLOCK)
1589                         return -ECHILD;
1590                 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
1591                 if (error)
1592                         return error;
1593                 unlock = 1;
1594         }
1595
1596         if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
1597                 error = -EACCES;
1598         else
1599                 error = generic_permission(inode, mask);
1600         if (unlock)
1601                 gfs2_glock_dq_uninit(&i_gh);
1602
1603         return error;
1604 }
1605
1606 static int __gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
1607 {
1608         setattr_copy(inode, attr);
1609         mark_inode_dirty(inode);
1610         return 0;
1611 }
1612
1613 /**
1614  * gfs2_setattr_simple -
1615  * @ip:
1616  * @attr:
1617  *
1618  * Returns: errno
1619  */
1620
1621 int gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
1622 {
1623         int error;
1624
1625         if (current->journal_info)
1626                 return __gfs2_setattr_simple(inode, attr);
1627
1628         error = gfs2_trans_begin(GFS2_SB(inode), RES_DINODE, 0);
1629         if (error)
1630                 return error;
1631
1632         error = __gfs2_setattr_simple(inode, attr);
1633         gfs2_trans_end(GFS2_SB(inode));
1634         return error;
1635 }
1636
1637 static int setattr_chown(struct inode *inode, struct iattr *attr)
1638 {
1639         struct gfs2_inode *ip = GFS2_I(inode);
1640         struct gfs2_sbd *sdp = GFS2_SB(inode);
1641         kuid_t ouid, nuid;
1642         kgid_t ogid, ngid;
1643         int error;
1644
1645         ouid = inode->i_uid;
1646         ogid = inode->i_gid;
1647         nuid = attr->ia_uid;
1648         ngid = attr->ia_gid;
1649
1650         if (!(attr->ia_valid & ATTR_UID) || uid_eq(ouid, nuid))
1651                 ouid = nuid = NO_UID_QUOTA_CHANGE;
1652         if (!(attr->ia_valid & ATTR_GID) || gid_eq(ogid, ngid))
1653                 ogid = ngid = NO_GID_QUOTA_CHANGE;
1654
1655         error = get_write_access(inode);
1656         if (error)
1657                 return error;
1658
1659         error = gfs2_rs_alloc(ip);
1660         if (error)
1661                 goto out;
1662
1663         error = gfs2_rindex_update(sdp);
1664         if (error)
1665                 goto out;
1666
1667         error = gfs2_quota_lock(ip, nuid, ngid);
1668         if (error)
1669                 goto out;
1670
1671         if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
1672             !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
1673                 error = gfs2_quota_check(ip, nuid, ngid);
1674                 if (error)
1675                         goto out_gunlock_q;
1676         }
1677
1678         error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
1679         if (error)
1680                 goto out_gunlock_q;
1681
1682         error = gfs2_setattr_simple(inode, attr);
1683         if (error)
1684                 goto out_end_trans;
1685
1686         if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
1687             !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
1688                 u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
1689                 gfs2_quota_change(ip, -blocks, ouid, ogid);
1690                 gfs2_quota_change(ip, blocks, nuid, ngid);
1691         }
1692
1693 out_end_trans:
1694         gfs2_trans_end(sdp);
1695 out_gunlock_q:
1696         gfs2_quota_unlock(ip);
1697 out:
1698         put_write_access(inode);
1699         return error;
1700 }
1701
1702 /**
1703  * gfs2_setattr - Change attributes on an inode
1704  * @dentry: The dentry which is changing
1705  * @attr: The structure describing the change
1706  *
1707  * The VFS layer wants to change one or more of an inodes attributes.  Write
1708  * that change out to disk.
1709  *
1710  * Returns: errno
1711  */
1712
1713 static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
1714 {
1715         struct inode *inode = dentry->d_inode;
1716         struct gfs2_inode *ip = GFS2_I(inode);
1717         struct gfs2_holder i_gh;
1718         int error;
1719
1720         error = gfs2_rs_alloc(ip);
1721         if (error)
1722                 return error;
1723
1724         error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1725         if (error)
1726                 return error;
1727
1728         error = -EPERM;
1729         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1730                 goto out;
1731
1732         error = inode_change_ok(inode, attr);
1733         if (error)
1734                 goto out;
1735
1736         if (attr->ia_valid & ATTR_SIZE)
1737                 error = gfs2_setattr_size(inode, attr->ia_size);
1738         else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1739                 error = setattr_chown(inode, attr);
1740         else {
1741                 error = gfs2_setattr_simple(inode, attr);
1742                 if (!error && attr->ia_valid & ATTR_MODE)
1743                         error = posix_acl_chmod(inode, inode->i_mode);
1744         }
1745
1746 out:
1747         if (!error)
1748                 mark_inode_dirty(inode);
1749         gfs2_glock_dq_uninit(&i_gh);
1750         return error;
1751 }
1752
1753 /**
1754  * gfs2_getattr - Read out an inode's attributes
1755  * @mnt: The vfsmount the inode is being accessed from
1756  * @dentry: The dentry to stat
1757  * @stat: The inode's stats
1758  *
1759  * This may be called from the VFS directly, or from within GFS2 with the
1760  * inode locked, so we look to see if the glock is already locked and only
1761  * lock the glock if its not already been done. Note that its the NFS
1762  * readdirplus operation which causes this to be called (from filldir)
1763  * with the glock already held.
1764  *
1765  * Returns: errno
1766  */
1767
1768 static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1769                         struct kstat *stat)
1770 {
1771         struct inode *inode = dentry->d_inode;
1772         struct gfs2_inode *ip = GFS2_I(inode);
1773         struct gfs2_holder gh;
1774         int error;
1775         int unlock = 0;
1776
1777         if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
1778                 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1779                 if (error)
1780                         return error;
1781                 unlock = 1;
1782         }
1783
1784         generic_fillattr(inode, stat);
1785         if (unlock)
1786                 gfs2_glock_dq_uninit(&gh);
1787
1788         return 0;
1789 }
1790
1791 static int gfs2_setxattr(struct dentry *dentry, const char *name,
1792                          const void *data, size_t size, int flags)
1793 {
1794         struct inode *inode = dentry->d_inode;
1795         struct gfs2_inode *ip = GFS2_I(inode);
1796         struct gfs2_holder gh;
1797         int ret;
1798
1799         gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1800         ret = gfs2_glock_nq(&gh);
1801         if (ret == 0) {
1802                 ret = gfs2_rs_alloc(ip);
1803                 if (ret == 0)
1804                         ret = generic_setxattr(dentry, name, data, size, flags);
1805                 gfs2_glock_dq(&gh);
1806         }
1807         gfs2_holder_uninit(&gh);
1808         return ret;
1809 }
1810
1811 static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1812                              void *data, size_t size)
1813 {
1814         struct inode *inode = dentry->d_inode;
1815         struct gfs2_inode *ip = GFS2_I(inode);
1816         struct gfs2_holder gh;
1817         int ret;
1818
1819         /* For selinux during lookup */
1820         if (gfs2_glock_is_locked_by_me(ip->i_gl))
1821                 return generic_getxattr(dentry, name, data, size);
1822
1823         gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1824         ret = gfs2_glock_nq(&gh);
1825         if (ret == 0) {
1826                 ret = generic_getxattr(dentry, name, data, size);
1827                 gfs2_glock_dq(&gh);
1828         }
1829         gfs2_holder_uninit(&gh);
1830         return ret;
1831 }
1832
1833 static int gfs2_removexattr(struct dentry *dentry, const char *name)
1834 {
1835         struct inode *inode = dentry->d_inode;
1836         struct gfs2_inode *ip = GFS2_I(inode);
1837         struct gfs2_holder gh;
1838         int ret;
1839
1840         gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1841         ret = gfs2_glock_nq(&gh);
1842         if (ret == 0) {
1843                 ret = gfs2_rs_alloc(ip);
1844                 if (ret == 0)
1845                         ret = generic_removexattr(dentry, name);
1846                 gfs2_glock_dq(&gh);
1847         }
1848         gfs2_holder_uninit(&gh);
1849         return ret;
1850 }
1851
1852 static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1853                        u64 start, u64 len)
1854 {
1855         struct gfs2_inode *ip = GFS2_I(inode);
1856         struct gfs2_holder gh;
1857         int ret;
1858
1859         ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1860         if (ret)
1861                 return ret;
1862
1863         mutex_lock(&inode->i_mutex);
1864
1865         ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
1866         if (ret)
1867                 goto out;
1868
1869         if (gfs2_is_stuffed(ip)) {
1870                 u64 phys = ip->i_no_addr << inode->i_blkbits;
1871                 u64 size = i_size_read(inode);
1872                 u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
1873                             FIEMAP_EXTENT_DATA_INLINE;
1874                 phys += sizeof(struct gfs2_dinode);
1875                 phys += start;
1876                 if (start + len > size)
1877                         len = size - start;
1878                 if (start < size)
1879                         ret = fiemap_fill_next_extent(fieinfo, start, phys,
1880                                                       len, flags);
1881                 if (ret == 1)
1882                         ret = 0;
1883         } else {
1884                 ret = __generic_block_fiemap(inode, fieinfo, start, len,
1885                                              gfs2_block_map);
1886         }
1887
1888         gfs2_glock_dq_uninit(&gh);
1889 out:
1890         mutex_unlock(&inode->i_mutex);
1891         return ret;
1892 }
1893
1894 const struct inode_operations gfs2_file_iops = {
1895         .permission = gfs2_permission,
1896         .setattr = gfs2_setattr,
1897         .getattr = gfs2_getattr,
1898         .setxattr = gfs2_setxattr,
1899         .getxattr = gfs2_getxattr,
1900         .listxattr = gfs2_listxattr,
1901         .removexattr = gfs2_removexattr,
1902         .fiemap = gfs2_fiemap,
1903         .get_acl = gfs2_get_acl,
1904         .set_acl = gfs2_set_acl,
1905 };
1906
1907 const struct inode_operations gfs2_dir_iops = {
1908         .create = gfs2_create,
1909         .lookup = gfs2_lookup,
1910         .link = gfs2_link,
1911         .unlink = gfs2_unlink,
1912         .symlink = gfs2_symlink,
1913         .mkdir = gfs2_mkdir,
1914         .rmdir = gfs2_unlink,
1915         .mknod = gfs2_mknod,
1916         .rename = gfs2_rename,
1917         .permission = gfs2_permission,
1918         .setattr = gfs2_setattr,
1919         .getattr = gfs2_getattr,
1920         .setxattr = gfs2_setxattr,
1921         .getxattr = gfs2_getxattr,
1922         .listxattr = gfs2_listxattr,
1923         .removexattr = gfs2_removexattr,
1924         .fiemap = gfs2_fiemap,
1925         .get_acl = gfs2_get_acl,
1926         .set_acl = gfs2_set_acl,
1927         .atomic_open = gfs2_atomic_open,
1928 };
1929
1930 const struct inode_operations gfs2_symlink_iops = {
1931         .readlink = generic_readlink,
1932         .follow_link = gfs2_follow_link,
1933         .put_link = kfree_put_link,
1934         .permission = gfs2_permission,
1935         .setattr = gfs2_setattr,
1936         .getattr = gfs2_getattr,
1937         .setxattr = gfs2_setxattr,
1938         .getxattr = gfs2_getxattr,
1939         .listxattr = gfs2_listxattr,
1940         .removexattr = gfs2_removexattr,
1941         .fiemap = gfs2_fiemap,
1942 };
1943