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