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