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