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