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