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