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