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