blob: 689ddb09e159621e5e35861f67a6937002ca0540 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehousef2741d92011-05-13 12:11:17 +01003 * Copyright (C) 2004-2011 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
9
David Teiglandb3b94fa2006-01-16 16:50:04 +000010#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>
David Teiglandb3b94fa2006-01-16 16:50:04 +000015#include <linux/mm.h>
16#include <linux/xattr.h>
17#include <linux/posix_acl.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050018#include <linux/gfs2_ondisk.h>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050019#include <linux/crc32.h>
Steven Whitehousee9079cc2008-10-14 14:43:29 +010020#include <linux/fiemap.h>
Steven Whitehouse194c0112011-05-09 14:06:38 +010021#include <linux/security.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000022#include <asm/uaccess.h>
23
24#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050025#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000026#include "acl.h"
27#include "bmap.h"
28#include "dir.h"
Steven Whitehouse307cf6e2009-08-26 18:51:04 +010029#include "xattr.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000030#include "glock.h"
31#include "inode.h"
32#include "meta_io.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000033#include "quota.h"
34#include "rgrp.h"
35#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050036#include "util.h"
Steven Whitehouseb2760582008-10-14 16:05:55 +010037#include "super.h"
Steven Whitehouse194c0112011-05-09 14:06:38 +010038#include "glops.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000039
Bob Petersonff342452015-03-27 08:25:41 -050040struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr)
Steven Whitehouse194c0112011-05-09 14:06:38 +010041{
Bob Petersonff342452015-03-27 08:25:41 -050042 return ilookup(sb, (unsigned long)no_addr);
Steven Whitehouse194c0112011-05-09 14:06:38 +010043}
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
Bob Petersonff342452015-03-27 08:25:41 -050096 inode = iget_locked(sb, (unsigned long)no_addr);
Steven Whitehouse194c0112011-05-09 14:06:38 +010097 ip = GFS2_I(inode);
Bob Petersonff342452015-03-27 08:25:41 -050098 ip->i_no_addr = no_addr;
Steven Whitehouse194c0112011-05-09 14:06:38 +010099
100 if (!inode)
Steven Whitehouseac3beb62014-01-16 10:31:13 +0000101 return ERR_PTR(-ENOMEM);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100102
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:
Bob Petersona6a4d982013-05-29 11:51:52 -0400141 ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100142 ip->i_iopen_gh.gh_gl->gl_object = NULL;
Bob Peterson86d067a2015-12-07 15:10:42 -0600143 gfs2_glock_dq_wait(&ip->i_iopen_gh);
144 gfs2_holder_uninit(&ip->i_iopen_gh);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100145fail_iopen:
146 if (io_gl)
147 gfs2_glock_put(io_gl);
148fail_put:
149 ip->i_gl->gl_object = NULL;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100150fail:
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 &&
David Howells2b0143b2015-03-17 22:25:59 +0000247 dir == d_inode(sb->s_root))) {
Steven Whitehouse194c0112011-05-09 14:06:38 +0100248 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) {
Al Viro10556cb2011-06-20 19:28:19 -0400260 error = gfs2_permission(dir, MAY_EXEC);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100261 if (error)
262 goto out;
263 }
264
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +0100265 inode = gfs2_dir_search(dir, name, false);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100266 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,
Al Viro175a4eb2011-07-26 03:30:54 -0400286 umode_t mode)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100287{
288 int error;
289
Al Viro10556cb2011-06-20 19:28:19 -0400290 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100291 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
Steven Whitehouse194c0112011-05-09 14:06:38 +0100298 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
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000306static void munge_mode_uid_gid(const struct gfs2_inode *dip,
307 struct inode *inode)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100308{
309 if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -0800310 (dip->i_inode.i_mode & S_ISUID) &&
311 !uid_eq(dip->i_inode.i_uid, GLOBAL_ROOT_UID)) {
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000312 if (S_ISDIR(inode->i_mode))
313 inode->i_mode |= S_ISUID;
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -0800314 else if (!uid_eq(dip->i_inode.i_uid, current_fsuid()))
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000315 inode->i_mode &= ~07111;
316 inode->i_uid = dip->i_inode.i_uid;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100317 } else
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000318 inode->i_uid = current_fsuid();
Steven Whitehouse194c0112011-05-09 14:06:38 +0100319
320 if (dip->i_inode.i_mode & S_ISGID) {
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000321 if (S_ISDIR(inode->i_mode))
322 inode->i_mode |= S_ISGID;
323 inode->i_gid = dip->i_inode.i_gid;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100324 } else
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000325 inode->i_gid = current_fsgid();
Steven Whitehouse194c0112011-05-09 14:06:38 +0100326}
327
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000328static int alloc_dinode(struct gfs2_inode *ip, u32 flags, unsigned *dblocks)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100329{
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000330 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000331 struct gfs2_alloc_parms ap = { .target = *dblocks, .aflags = flags, };
Steven Whitehouse194c0112011-05-09 14:06:38 +0100332 int error;
333
Abhi Dasb8fbf472015-03-18 12:03:41 -0500334 error = gfs2_quota_lock_check(ip, &ap);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100335 if (error)
336 goto out;
337
Steven Whitehouse7b9cff42013-10-02 11:13:25 +0100338 error = gfs2_inplace_reserve(ip, &ap);
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000339 if (error)
340 goto out_quota;
341
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000342 error = gfs2_trans_begin(sdp, (*dblocks * RES_RG_BIT) + RES_STATFS + RES_QUOTA, 0);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100343 if (error)
344 goto out_ipreserv;
345
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000346 error = gfs2_alloc_blocks(ip, &ip->i_no_addr, dblocks, 1, &ip->i_generation);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000347 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;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100350
351 gfs2_trans_end(sdp);
352
353out_ipreserv:
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000354 gfs2_inplace_release(ip);
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000355out_quota:
356 gfs2_quota_unlock(ip);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100357out:
Steven Whitehouse194c0112011-05-09 14:06:38 +0100358 return error;
359}
360
Steven Whitehousef2741d92011-05-13 12:11:17 +0100361static void gfs2_init_dir(struct buffer_head *dibh,
362 const struct gfs2_inode *parent)
Steven Whitehousee2d0a132011-05-13 09:55:55 +0100363{
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
Steven Whitehouse194c0112011-05-09 14:06:38 +0100378/**
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000379 * 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
405/**
Steven Whitehouse194c0112011-05-09 14:06:38 +0100406 * init_dinode - Fill in a new dinode structure
Steven Whitehousef2741d92011-05-13 12:11:17 +0100407 * @dip: The directory this inode is being created in
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000408 * @ip: The inode
Steven Whitehousef2741d92011-05-13 12:11:17 +0100409 * @symname: The symlink destination (if a symlink)
Steven Whitehousef2741d92011-05-13 12:11:17 +0100410 * @bhp: The buffer head (returned to caller)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100411 *
412 */
413
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000414static void init_dinode(struct gfs2_inode *dip, struct gfs2_inode *ip,
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000415 const char *symname)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100416{
Steven Whitehouse194c0112011-05-09 14:06:38 +0100417 struct gfs2_dinode *di;
418 struct buffer_head *dibh;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100419
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000420 dibh = gfs2_meta_new(ip->i_gl, ip->i_no_addr);
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000421 gfs2_trans_add_meta(ip->i_gl, dibh);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100422 di = (struct gfs2_dinode *)dibh->b_data;
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000423 gfs2_dinode_out(ip, di);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100424
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000425 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));
Steven Whitehouse194c0112011-05-09 14:06:38 +0100427 di->__pad1 = 0;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100428 di->__pad2 = 0;
429 di->__pad3 = 0;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100430 memset(&di->__pad4, 0, sizeof(di->__pad4));
Steven Whitehouse194c0112011-05-09 14:06:38 +0100431 memset(&di->di_reserved, 0, sizeof(di->di_reserved));
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000432 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
Steven Whitehouse160b4022011-05-13 10:34:59 +0100433
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000434 switch(ip->i_inode.i_mode & S_IFMT) {
Steven Whitehouse160b4022011-05-13 10:34:59 +0100435 case S_IFDIR:
Steven Whitehousee2d0a132011-05-13 09:55:55 +0100436 gfs2_init_dir(dibh, dip);
Steven Whitehouse160b4022011-05-13 10:34:59 +0100437 break;
438 case S_IFLNK:
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000439 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, ip->i_inode.i_size);
Steven Whitehouse160b4022011-05-13 10:34:59 +0100440 break;
Steven Whitehousee2d0a132011-05-13 09:55:55 +0100441 }
442
Steven Whitehouse194c0112011-05-09 14:06:38 +0100443 set_buffer_uptodate(dibh);
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000444 brelse(dibh);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100445}
446
Steven Whitehouse534cf9c2014-01-06 12:03:05 +0000447/**
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
Steven Whitehouse194c0112011-05-09 14:06:38 +0100469static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000470 struct gfs2_inode *ip, struct gfs2_diradd *da)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100471{
472 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000473 struct gfs2_alloc_parms ap = { .target = da->nr_blocks, };
Steven Whitehouse194c0112011-05-09 14:06:38 +0100474 int error;
475
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000476 if (da->nr_blocks) {
Abhi Dasb8fbf472015-03-18 12:03:41 -0500477 error = gfs2_quota_lock_check(dip, &ap);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100478 if (error)
479 goto fail_quota_locks;
480
Steven Whitehouse7b9cff42013-10-02 11:13:25 +0100481 error = gfs2_inplace_reserve(dip, &ap);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100482 if (error)
483 goto fail_quota_locks;
484
Steven Whitehouse534cf9c2014-01-06 12:03:05 +0000485 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, da, 2), 0);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100486 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
Steven Whitehouse2b47dad2014-01-06 12:49:43 +0000494 error = gfs2_dir_add(&dip->i_inode, name, ip, da);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100495
Steven Whitehouse194c0112011-05-09 14:06:38 +0100496 gfs2_trans_end(sdp);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100497fail_ipreserv:
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000498 gfs2_inplace_release(dip);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100499fail_quota_locks:
500 gfs2_quota_unlock(dip);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100501 return error;
502}
503
H Hartley Sweeten46cc1e52011-09-23 15:51:32 -0700504static int gfs2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
Mimi Zohar9d8f13b2011-06-06 15:29:25 -0400505 void *fs_info)
506{
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;
516 }
517 return err;
518}
519
Steven Whitehouse194c0112011-05-09 14:06:38 +0100520/**
Steven Whitehousef2741d92011-05-13 12:11:17 +0100521 * gfs2_create_inode - Create a new inode
522 * @dir: The parent directory
523 * @dentry: The new dentry
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100524 * @file: If non-NULL, the file which is being opened
Steven Whitehousef2741d92011-05-13 12:11:17 +0100525 * @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)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100529 *
Steven Whitehousef2741d92011-05-13 12:11:17 +0100530 * Returns: 0 on success, or error code
Steven Whitehouse194c0112011-05-09 14:06:38 +0100531 */
532
Steven Whitehousef2741d92011-05-13 12:11:17 +0100533static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100534 struct file *file,
Al Viro175a4eb2011-07-26 03:30:54 -0400535 umode_t mode, dev_t dev, const char *symname,
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100536 unsigned int size, int excl, int *opened)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100537{
Steven Whitehousef2741d92011-05-13 12:11:17 +0100538 const struct qstr *name = &dentry->d_name;
Christoph Hellwige01580b2013-12-20 05:16:52 -0800539 struct posix_acl *default_acl, *acl;
Steven Whitehousef2741d92011-05-13 12:11:17 +0100540 struct gfs2_holder ghs[2];
Steven Whitehouse194c0112011-05-09 14:06:38 +0100541 struct inode *inode = NULL;
Bob Peterson8e2e0042012-07-19 08:12:40 -0400542 struct gfs2_inode *dip = GFS2_I(dir), *ip;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100543 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Bob Petersona4923862015-12-07 16:24:27 -0600544 struct gfs2_glock *io_gl = NULL;
Bob Peterson783013c2015-12-04 10:19:14 -0600545 int error, free_vfs_inode = 1;
Steven Whitehouse9dbe9612012-10-31 10:37:10 +0000546 u32 aflags = 0;
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000547 unsigned blocks = 1;
Bob Peterson19aeb5a2014-09-29 08:52:04 -0400548 struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, };
Steven Whitehouse194c0112011-05-09 14:06:38 +0100549
550 if (!name->len || name->len > GFS2_FNAMESIZE)
Steven Whitehousef2741d92011-05-13 12:11:17 +0100551 return -ENAMETOOLONG;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100552
Bob Petersonb54e9a02015-10-26 10:40:28 -0500553 error = gfs2_rsqa_alloc(dip);
Bob Peterson0a305e42012-06-06 11:17:59 +0100554 if (error)
555 return error;
556
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000557 error = gfs2_rindex_update(sdp);
558 if (error)
559 return error;
560
Steven Whitehousef2741d92011-05-13 12:11:17 +0100561 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100562 if (error)
563 goto fail;
564
565 error = create_ok(dip, name, mode);
566 if (error)
567 goto fail_gunlock;
568
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +0100569 inode = gfs2_dir_search(dir, &dentry->d_name, !S_ISREG(mode) || excl);
570 error = PTR_ERR(inode);
571 if (!IS_ERR(inode)) {
Al Viro571a4b52014-11-19 19:34:49 +0000572 if (S_ISDIR(inode->i_mode)) {
573 iput(inode);
574 inode = ERR_PTR(-EISDIR);
575 goto fail_gunlock;
576 }
Al Viro44bb31b2014-11-19 19:35:24 +0000577 d_instantiate(dentry, inode);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100578 error = 0;
Miklos Szeredi0d0d1102013-09-16 14:52:00 +0200579 if (file) {
Al Viro44bb31b2014-11-19 19:35:24 +0000580 if (S_ISREG(inode->i_mode))
Miklos Szeredi5ca1db42013-09-23 13:21:04 +0100581 error = finish_open(file, dentry, gfs2_open_common, opened);
Al Viro44bb31b2014-11-19 19:35:24 +0000582 else
583 error = finish_no_open(file, NULL);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100584 }
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +0100585 gfs2_glock_dq_uninit(ghs);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100586 return error;
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +0100587 } else if (error != -ENOENT) {
588 goto fail_gunlock;
589 }
590
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000591 error = gfs2_diradd_alloc_required(dir, name, &da);
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000592 if (error < 0)
593 goto fail_gunlock;
594
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000595 inode = new_inode(sdp->sd_vfs);
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000596 error = -ENOMEM;
597 if (!inode)
598 goto fail_gunlock;
599
Christoph Hellwige01580b2013-12-20 05:16:52 -0800600 error = posix_acl_create(dir, &mode, &default_acl, &acl);
601 if (error)
Bob Peterson783013c2015-12-04 10:19:14 -0600602 goto fail_gunlock;
Christoph Hellwige01580b2013-12-20 05:16:52 -0800603
Bob Peterson8e2e0042012-07-19 08:12:40 -0400604 ip = GFS2_I(inode);
Bob Petersonb54e9a02015-10-26 10:40:28 -0500605 error = gfs2_rsqa_alloc(ip);
Steven Whitehouseff7f4cb2012-09-10 10:03:50 +0100606 if (error)
Christoph Hellwige01580b2013-12-20 05:16:52 -0800607 goto fail_free_acls;
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000608
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000609 inode->i_mode = mode;
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000610 set_nlink(inode, S_ISDIR(mode) ? 2 : 1);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000611 inode->i_rdev = dev;
612 inode->i_size = size;
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000613 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Steven Whitehouse28fb3022013-02-26 19:09:35 +0000614 gfs2_set_inode_blocks(inode, 1);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000615 munge_mode_uid_gid(dip, inode);
Abhi Das00a158b2014-09-18 21:40:28 -0500616 check_and_update_goal(dip);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000617 ip->i_goal = dip->i_goal;
Steven Whitehouse28fb3022013-02-26 19:09:35 +0000618 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 }
Abhi Dasacc546f2015-11-10 15:07:26 -0600637
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
Steven Whitehouse28fb3022013-02-26 19:09:35 +0000642 gfs2_set_inode_flags(inode);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000643
David Howells2b0143b2015-03-17 22:25:59 +0000644 if ((GFS2_I(d_inode(sdp->sd_root_dir)) == dip) ||
Steven Whitehouse9dbe9612012-10-31 10:37:10 +0000645 (dip->i_diskflags & GFS2_DIF_TOPDIR))
646 aflags |= GFS2_AF_ORLOV;
647
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000648 if (default_acl || acl)
649 blocks++;
650
651 error = alloc_dinode(ip, aflags, &blocks);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000652 if (error)
653 goto fail_free_inode;
654
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000655 gfs2_set_inode_blocks(inode, blocks);
656
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000657 error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
658 if (error)
659 goto fail_free_inode;
660
Bob Peterson1e2d9d42012-11-21 09:56:00 -0500661 ip->i_gl->gl_object = ip;
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000662 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
663 if (error)
664 goto fail_free_inode;
665
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000666 error = gfs2_trans_begin(sdp, blocks, 0);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000667 if (error)
Steven Whitehouseff7f4cb2012-09-10 10:03:50 +0100668 goto fail_gunlock2;
Bob Peterson0a305e42012-06-06 11:17:59 +0100669
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000670 if (blocks > 1) {
671 ip->i_eattr = ip->i_no_addr + 1;
672 gfs2_init_xattr(ip);
673 }
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000674 init_dinode(dip, ip, symname);
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000675 gfs2_trans_end(sdp);
676
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000677 error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
678 if (error)
679 goto fail_gunlock2;
680
Bob Petersona4923862015-12-07 16:24:27 -0600681 BUG_ON(test_and_set_bit(GLF_INODE_CREATING, &io_gl->gl_flags));
682
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000683 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
684 if (error)
685 goto fail_gunlock2;
686
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
Bob Peterson783013c2015-12-04 10:19:14 -0600692 free_vfs_inode = 0; /* After this point, the inode is no longer
693 considered free. Any failures need to undo
694 the gfs2 structures. */
Christoph Hellwige01580b2013-12-20 05:16:52 -0800695 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
Steven Whitehouse194c0112011-05-09 14:06:38 +0100705 if (error)
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000706 goto fail_gunlock3;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100707
Bob Petersonf45dc262014-03-19 09:37:00 -0400708 error = security_inode_init_security(&ip->i_inode, &dip->i_inode, name,
709 &gfs2_initxattrs, NULL);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100710 if (error)
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000711 goto fail_gunlock3;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100712
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000713 error = link_dinode(dip, name, ip, &da);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100714 if (error)
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000715 goto fail_gunlock3;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100716
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000717 mark_inode_dirty(inode);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100718 d_instantiate(dentry, inode);
Miklos Szeredic5bf8fe2013-09-16 14:52:03 +0200719 if (file) {
720 *opened |= FILE_CREATED;
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100721 error = finish_open(file, dentry, gfs2_open_common, opened);
Miklos Szeredic5bf8fe2013-09-16 14:52:03 +0200722 }
Steven Whitehouse28fb3022013-02-26 19:09:35 +0000723 gfs2_glock_dq_uninit(ghs);
724 gfs2_glock_dq_uninit(ghs + 1);
Bob Petersona4923862015-12-07 16:24:27 -0600725 clear_bit(GLF_INODE_CREATING, &io_gl->gl_flags);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100726 return error;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100727
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000728fail_gunlock3:
Bob Peterson783013c2015-12-04 10:19:14 -0600729 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
730 gfs2_glock_put(io_gl);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100731fail_gunlock2:
Bob Petersona4923862015-12-07 16:24:27 -0600732 if (io_gl)
733 clear_bit(GLF_INODE_CREATING, &io_gl->gl_flags);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100734 gfs2_glock_dq_uninit(ghs + 1);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000735fail_free_inode:
736 if (ip->i_gl)
737 gfs2_glock_put(ip->i_gl);
Bob Petersonb54e9a02015-10-26 10:40:28 -0500738 gfs2_rsqa_delete(ip, NULL);
Christoph Hellwige01580b2013-12-20 05:16:52 -0800739fail_free_acls:
740 if (default_acl)
741 posix_acl_release(default_acl);
742 if (acl)
743 posix_acl_release(acl);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100744fail_gunlock:
Steven Whitehouse2b47dad2014-01-06 12:49:43 +0000745 gfs2_dir_no_add(&da);
Steven Whitehousef2741d92011-05-13 12:11:17 +0100746 gfs2_glock_dq_uninit(ghs);
Steven Whitehouse40ac2182011-08-02 13:17:27 +0100747 if (inode && !IS_ERR(inode)) {
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000748 clear_nlink(inode);
Abhi Das05978802014-03-31 10:33:17 -0500749 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);
Steven Whitehouse40ac2182011-08-02 13:17:27 +0100753 iput(inode);
754 }
Steven Whitehouse194c0112011-05-09 14:06:38 +0100755fail:
Steven Whitehousef2741d92011-05-13 12:11:17 +0100756 return error;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100757}
Steven Whitehousef2741d92011-05-13 12:11:17 +0100758
David Teiglandb3b94fa2006-01-16 16:50:04 +0000759/**
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,
Al Viroebfc3b42012-06-10 18:05:36 -0400769 umode_t mode, bool excl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000770{
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100771 return gfs2_create_inode(dir, dentry, NULL, S_IFREG | mode, 0, NULL, 0, excl, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000772}
773
774/**
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100775 * __gfs2_lookup - Look up a filename in a directory and return its inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000776 * @dir: The directory inode
777 * @dentry: The dentry of the new inode
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100778 * @file: File to be opened
779 * @opened: atomic_open flags
David Teiglandb3b94fa2006-01-16 16:50:04 +0000780 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000781 *
782 * Returns: errno
783 */
784
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100785static struct dentry *__gfs2_lookup(struct inode *dir, struct dentry *dentry,
786 struct file *file, int *opened)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000787{
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100788 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);
Benjamin Coddington7b7a9112014-09-10 14:09:20 -0400795 if (inode == NULL) {
796 d_add(dentry, NULL);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100797 return NULL;
Benjamin Coddington7b7a9112014-09-10 14:09:20 -0400798 }
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100799 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);
Steven Whitehouse9656b2c2008-01-08 08:14:30 +0000807 }
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100808
809 d = d_splice_alias(inode, dentry);
J. Bruce Fieldsd57b9c92014-01-16 13:51:07 -0500810 if (IS_ERR(d)) {
J. Bruce Fieldsd57b9c92014-01-16 13:51:07 -0500811 gfs2_glock_dq_uninit(&gh);
812 return d;
813 }
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100814 if (file && S_ISREG(inode->i_mode))
815 error = finish_open(file, dentry, gfs2_open_common, opened);
816
817 gfs2_glock_dq_uninit(&gh);
Miklos Szeredi5ca1db42013-09-23 13:21:04 +0100818 if (error) {
819 dput(d);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100820 return ERR_PTR(error);
Miklos Szeredi5ca1db42013-09-23 13:21:04 +0100821 }
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100822 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);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000829}
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{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400846 struct gfs2_inode *dip = GFS2_I(dir);
847 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Howells2b0143b2015-03-17 22:25:59 +0000848 struct inode *inode = d_inode(old_dentry);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400849 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000850 struct gfs2_holder ghs[2];
Steven Whitehouse2baee032011-05-09 12:08:36 +0100851 struct buffer_head *dibh;
Bob Peterson19aeb5a2014-09-29 08:52:04 -0400852 struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000853 int error;
854
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500855 if (S_ISDIR(inode->i_mode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000856 return -EPERM;
857
Bob Petersonb54e9a02015-10-26 10:40:28 -0500858 error = gfs2_rsqa_alloc(dip);
Bob Peterson0a305e42012-06-06 11:17:59 +0100859 if (error)
860 return error;
861
David Teiglandb3b94fa2006-01-16 16:50:04 +0000862 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
Bob Peterson72dbf472008-08-12 13:39:29 -0500865 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000866 if (error)
Bob Peterson72dbf472008-08-12 13:39:29 -0500867 goto out_parent;
868
869 error = gfs2_glock_nq(ghs + 1); /* child */
870 if (error)
871 goto out_child;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000872
Steven Whitehoused192a8e2011-05-05 12:35:40 +0100873 error = -ENOENT;
874 if (inode->i_nlink == 0)
875 goto out_gunlock;
876
Al Viro10556cb2011-06-20 19:28:19 -0400877 error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000878 if (error)
879 goto out_gunlock;
880
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100881 error = gfs2_dir_check(dir, &dentry->d_name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000882 switch (error) {
883 case -ENOENT:
884 break;
885 case 0:
886 error = -EEXIST;
887 default:
888 goto out_gunlock;
889 }
890
891 error = -EINVAL;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500892 if (!dip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000893 goto out_gunlock;
894 error = -EFBIG;
Steven Whitehousead6203f2008-11-03 13:59:19 +0000895 if (dip->i_entries == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000896 goto out_gunlock;
897 error = -EPERM;
898 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
899 goto out_gunlock;
900 error = -EINVAL;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500901 if (!ip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000902 goto out_gunlock;
903 error = -EMLINK;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500904 if (ip->i_inode.i_nlink == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000905 goto out_gunlock;
906
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000907 error = gfs2_diradd_alloc_required(dir, &dentry->d_name, &da);
Steven Whitehousec7526662006-03-20 12:30:04 -0500908 if (error < 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000909 goto out_gunlock;
910
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000911 if (da.nr_blocks) {
912 struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
Abhi Dasb8fbf472015-03-18 12:03:41 -0500913 error = gfs2_quota_lock_check(dip, &ap);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000914 if (error)
Bob Peterson5407e242012-05-18 09:28:23 -0400915 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000916
Steven Whitehouse7b9cff42013-10-02 11:13:25 +0100917 error = gfs2_inplace_reserve(dip, &ap);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000918 if (error)
919 goto out_gunlock_q;
920
Steven Whitehouse534cf9c2014-01-06 12:03:05 +0000921 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, &da, 2), 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000922 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
Steven Whitehouse2baee032011-05-09 12:08:36 +0100930 error = gfs2_meta_inode_buffer(ip, &dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000931 if (error)
932 goto out_end_trans;
933
Steven Whitehouse2b47dad2014-01-06 12:49:43 +0000934 error = gfs2_dir_add(dir, &dentry->d_name, ip, &da);
Steven Whitehouse2baee032011-05-09 12:08:36 +0100935 if (error)
936 goto out_brelse;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000937
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000938 gfs2_trans_add_meta(ip->i_gl, dibh);
Steven Whitehouse2baee032011-05-09 12:08:36 +0100939 inc_nlink(&ip->i_inode);
940 ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100941 ihold(inode);
942 d_instantiate(dentry, inode);
943 mark_inode_dirty(inode);
Steven Whitehouse2baee032011-05-09 12:08:36 +0100944
945out_brelse:
946 brelse(dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400947out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000948 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400949out_ipres:
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000950 if (da.nr_blocks)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000951 gfs2_inplace_release(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400952out_gunlock_q:
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000953 if (da.nr_blocks)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000954 gfs2_quota_unlock(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400955out_gunlock:
Steven Whitehouse2b47dad2014-01-06 12:49:43 +0000956 gfs2_dir_no_add(&da);
Bob Peterson72dbf472008-08-12 13:39:29 -0500957 gfs2_glock_dq(ghs + 1);
958out_child:
959 gfs2_glock_dq(ghs);
960out_parent:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000961 gfs2_holder_uninit(ghs);
962 gfs2_holder_uninit(ghs + 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000963 return error;
964}
965
Steven Whitehouse87ec2172009-05-22 10:54:50 +0100966/*
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) &&
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -0800986 !uid_eq(dip->i_inode.i_uid, current_fsuid()) &&
987 !uid_eq(ip->i_inode.i_uid, current_fsuid()) && !capable(CAP_FOWNER))
Steven Whitehouse87ec2172009-05-22 10:54:50 +0100988 return -EPERM;
989
990 if (IS_APPEND(&dip->i_inode))
991 return -EPERM;
992
Al Viro10556cb2011-06-20 19:28:19 -0400993 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
Steven Whitehouse87ec2172009-05-22 10:54:50 +0100994 if (error)
995 return error;
996
Fabian Frederick37975f12014-10-09 22:49:21 +0200997 return gfs2_dir_check(&dip->i_inode, name, ip);
Steven Whitehouse87ec2172009-05-22 10:54:50 +0100998}
999
David Teiglandb3b94fa2006-01-16 16:50:04 +00001000/**
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001001 * 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
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001004 * @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,
Bob Peterson4327a9b2012-11-12 13:03:29 -05001013 const struct dentry *dentry)
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001014{
David Howells2b0143b2015-03-17 22:25:59 +00001015 struct inode *inode = d_inode(dentry);
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001016 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);
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001029 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
David Teiglandb3b94fa2006-01-16 16:50:04 +00001039 * @dentry: The file itself
1040 *
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001041 * This routine uses the type of the inode as a flag to figure out
1042 * whether this is an unlink or an rmdir.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001043 *
1044 * Returns: errno
1045 */
1046
1047static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
1048{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001049 struct gfs2_inode *dip = GFS2_I(dir);
1050 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Howells2b0143b2015-03-17 22:25:59 +00001051 struct inode *inode = d_inode(dentry);
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001052 struct gfs2_inode *ip = GFS2_I(inode);
Russell Cattelanddee7602007-01-29 17:13:44 -06001053 struct gfs2_holder ghs[3];
1054 struct gfs2_rgrpd *rgd;
Bob Peterson5e2f7d62012-04-04 22:11:16 -04001055 int error;
1056
1057 error = gfs2_rindex_update(sdp);
1058 if (error)
1059 return error;
1060
1061 error = -EROFS;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001062
Russell Cattelanddee7602007-01-29 17:13:44 -06001063 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
1064 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
1065
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001066 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1);
Steven Whitehousea365fbf2012-02-24 15:09:14 +00001067 if (!rgd)
Steven Whitehouse87654892011-11-08 14:04:20 +00001068 goto out_inodes;
Steven Whitehousea365fbf2012-02-24 15:09:14 +00001069
Russell Cattelanddee7602007-01-29 17:13:44 -06001070 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
1071
1072
Steven Whitehouse8497a462007-08-26 14:23:56 +01001073 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001074 if (error)
Steven Whitehouse8497a462007-08-26 14:23:56 +01001075 goto out_parent;
1076
1077 error = gfs2_glock_nq(ghs + 1); /* child */
1078 if (error)
1079 goto out_child;
1080
Steven Whitehoused192a8e2011-05-05 12:35:40 +01001081 error = -ENOENT;
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001082 if (inode->i_nlink == 0)
Steven Whitehoused192a8e2011-05-05 12:35:40 +01001083 goto out_rgrp;
1084
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001085 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
Steven Whitehouse8497a462007-08-26 14:23:56 +01001091 error = gfs2_glock_nq(ghs + 2); /* rgrp */
1092 if (error)
1093 goto out_rgrp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001094
1095 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
1096 if (error)
Bob Peterson72dbf472008-08-12 13:39:29 -05001097 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001098
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001099 error = gfs2_trans_begin(sdp, 2*RES_DINODE + 3*RES_LEAF + RES_RG_BIT, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001100 if (error)
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001101 goto out_end_trans;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001102
Bob Peterson4327a9b2012-11-12 13:03:29 -05001103 error = gfs2_unlink_inode(dip, dentry);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001104
1105out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001106 gfs2_trans_end(sdp);
Bob Peterson72dbf472008-08-12 13:39:29 -05001107out_gunlock:
Steven Whitehouse8497a462007-08-26 14:23:56 +01001108 gfs2_glock_dq(ghs + 2);
1109out_rgrp:
Steven Whitehouse8497a462007-08-26 14:23:56 +01001110 gfs2_glock_dq(ghs + 1);
1111out_child:
Steven Whitehouse8497a462007-08-26 14:23:56 +01001112 gfs2_glock_dq(ghs);
1113out_parent:
Steven Whitehouse87654892011-11-08 14:04:20 +00001114 gfs2_holder_uninit(ghs + 2);
1115out_inodes:
1116 gfs2_holder_uninit(ghs + 1);
Steven Whitehouse8497a462007-08-26 14:23:56 +01001117 gfs2_holder_uninit(ghs);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001118 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{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001133 struct gfs2_sbd *sdp = GFS2_SB(dir);
Steven Whitehouse160b4022011-05-13 10:34:59 +01001134 unsigned int size;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001135
David Teiglandb3b94fa2006-01-16 16:50:04 +00001136 size = strlen(symname);
1137 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
1138 return -ENAMETOOLONG;
1139
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001140 return gfs2_create_inode(dir, dentry, NULL, S_IFLNK | S_IRWXUGO, 0, symname, size, 0, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001141}
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
Al Viro18bb1db2011-07-26 01:41:39 -04001152static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001153{
Steven Whitehouse28fb3022013-02-26 19:09:35 +00001154 struct gfs2_sbd *sdp = GFS2_SB(dir);
1155 unsigned dsize = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001156 return gfs2_create_inode(dir, dentry, NULL, S_IFDIR | mode, 0, NULL, dsize, 0, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001157}
1158
1159/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001160 * 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
Steven Whitehousef2741d92011-05-13 12:11:17 +01001164 * @dev: The device specification of the special file
David Teiglandb3b94fa2006-01-16 16:50:04 +00001165 *
1166 */
1167
Al Viro1a67aaf2011-07-26 01:52:52 -04001168static int gfs2_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001169 dev_t dev)
1170{
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001171 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,
Antonio Ospite86fbca42015-05-01 12:54:38 -05001187 struct file *file, unsigned flags,
1188 umode_t mode, int *opened)
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001189{
1190 struct dentry *d;
1191 bool excl = !!(flags & O_EXCL);
1192
Al Viro4d93bc32014-09-12 18:21:05 -04001193 if (!d_unhashed(dentry))
1194 goto skip_lookup;
1195
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001196 d = __gfs2_lookup(dir, dentry, file, opened);
1197 if (IS_ERR(d))
1198 return PTR_ERR(d);
Miklos Szeredi5ca1db42013-09-23 13:21:04 +01001199 if (d != NULL)
1200 dentry = d;
David Howells2b0143b2015-03-17 22:25:59 +00001201 if (d_really_is_positive(dentry)) {
Al Viroec7d8792014-11-19 19:35:58 +00001202 if (!(*opened & FILE_OPENED))
1203 return finish_no_open(file, d);
Miklos Szeredi5ca1db42013-09-23 13:21:04 +01001204 dput(d);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001205 return 0;
1206 }
1207
Miklos Szeredi5ca1db42013-09-23 13:21:04 +01001208 BUG_ON(d != NULL);
Al Viro4d93bc32014-09-12 18:21:05 -04001209
1210skip_lookup:
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001211 if (!(flags & O_CREAT))
1212 return -ENOENT;
1213
1214 return gfs2_create_inode(dir, dentry, file, S_IFREG | mode, 0, NULL, 0, excl, opened);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001215}
1216
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001217/*
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;
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001233 int error = 0;
1234
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001235 igrab(dir);
1236
1237 for (;;) {
1238 if (dir == &this->i_inode) {
1239 error = -EINVAL;
1240 break;
1241 }
David Howells2b0143b2015-03-17 22:25:59 +00001242 if (dir == d_inode(sb->s_root)) {
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001243 error = 0;
1244 break;
1245 }
1246
Steven Whitehouse8d123582010-09-17 12:30:23 +01001247 tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
Abhi Das48f8f712014-03-12 03:41:44 -05001248 if (!tmp) {
1249 error = -ENOENT;
1250 break;
1251 }
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001252 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
David Teiglandb3b94fa2006-01-16 16:50:04 +00001266/**
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001267 * 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
1295/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001296 * 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{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001308 struct gfs2_inode *odip = GFS2_I(odir);
1309 struct gfs2_inode *ndip = GFS2_I(ndir);
David Howells2b0143b2015-03-17 22:25:59 +00001310 struct gfs2_inode *ip = GFS2_I(d_inode(odentry));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001311 struct gfs2_inode *nip = NULL;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001312 struct gfs2_sbd *sdp = GFS2_SB(odir);
Steven Whitehouse8339ee52011-08-31 16:38:29 +01001313 struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, };
Russell Cattelanddee7602007-01-29 17:13:44 -06001314 struct gfs2_rgrpd *nrgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001315 unsigned int num_gh;
1316 int dir_rename = 0;
Bob Peterson19aeb5a2014-09-29 08:52:04 -04001317 struct gfs2_diradd da = { .nr_blocks = 0, .save_loc = 0, };
David Teiglandb3b94fa2006-01-16 16:50:04 +00001318 unsigned int x;
1319 int error;
1320
David Howells2b0143b2015-03-17 22:25:59 +00001321 if (d_really_is_positive(ndentry)) {
1322 nip = GFS2_I(d_inode(ndentry));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001323 if (ip == nip)
1324 return 0;
1325 }
1326
Bob Peterson5e2f7d62012-04-04 22:11:16 -04001327 error = gfs2_rindex_update(sdp);
1328 if (error)
1329 return error;
1330
Bob Petersonb54e9a02015-10-26 10:40:28 -05001331 error = gfs2_rsqa_alloc(ndip);
Bob Peterson0a305e42012-06-06 11:17:59 +01001332 if (error)
1333 return error;
1334
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001335 if (odip != ndip) {
1336 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
1337 0, &r_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001338 if (error)
1339 goto out;
1340
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001341 if (S_ISDIR(ip->i_inode.i_mode)) {
1342 dir_rename = 1;
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001343 /* don't move a directory into its subdir */
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001344 error = gfs2_ok_to_move(ip, ndip);
1345 if (error)
1346 goto out_gunlock_r;
1347 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001348 }
1349
Steven Whitehoused9d1ca32006-06-21 15:38:17 -04001350 num_gh = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001351 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -04001352 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++;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001358
Steven Whitehoused9d1ca32006-06-21 15:38:17 -04001359 if (nip) {
1360 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
1361 num_gh++;
Russell Cattelanddee7602007-01-29 17:13:44 -06001362 /* 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 */
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001366 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr, 1);
Russell Cattelanddee7602007-01-29 17:13:44 -06001367 if (nrgd)
1368 gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -04001369 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001370
Bob Peterson72dbf472008-08-12 13:39:29 -05001371 for (x = 0; x < num_gh; x++) {
1372 error = gfs2_glock_nq(ghs + x);
1373 if (error)
1374 goto out_gunlock;
1375 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001376
Steven Whitehoused192a8e2011-05-05 12:35:40 +01001377 error = -ENOENT;
1378 if (ip->i_inode.i_nlink == 0)
1379 goto out_gunlock;
1380
David Teiglandb3b94fa2006-01-16 16:50:04 +00001381 /* 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
Steven Whitehoused192a8e2011-05-05 12:35:40 +01001394 if (nip->i_inode.i_nlink == 0) {
1395 error = -EAGAIN;
1396 goto out_gunlock;
1397 }
1398
Steven Whitehouseb60623c2006-11-01 12:22:46 -05001399 if (S_ISDIR(nip->i_inode.i_mode)) {
Steven Whitehousead6203f2008-11-03 13:59:19 +00001400 if (nip->i_entries < 2) {
Steven Whitehouse94fb7632011-05-09 13:36:10 +01001401 gfs2_consist_inode(nip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001402 error = -EIO;
1403 goto out_gunlock;
1404 }
Steven Whitehousead6203f2008-11-03 13:59:19 +00001405 if (nip->i_entries > 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001406 error = -ENOTEMPTY;
1407 goto out_gunlock;
1408 }
1409 }
1410 } else {
Al Viro10556cb2011-06-20 19:28:19 -04001411 error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001412 if (error)
1413 goto out_gunlock;
1414
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001415 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001416 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) {
Steven Whitehouse4f561102006-11-01 14:04:17 -05001427 if (!ndip->i_inode.i_nlink) {
Steven Whitehoused192a8e2011-05-05 12:35:40 +01001428 error = -ENOENT;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001429 goto out_gunlock;
1430 }
Steven Whitehousead6203f2008-11-03 13:59:19 +00001431 if (ndip->i_entries == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001432 error = -EFBIG;
1433 goto out_gunlock;
1434 }
Steven Whitehouseb60623c2006-11-01 12:22:46 -05001435 if (S_ISDIR(ip->i_inode.i_mode) &&
Steven Whitehouse4f561102006-11-01 14:04:17 -05001436 ndip->i_inode.i_nlink == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001437 error = -EMLINK;
1438 goto out_gunlock;
1439 }
1440 }
1441 }
1442
1443 /* Check out the dir to be renamed */
1444
1445 if (dir_rename) {
David Howells2b0143b2015-03-17 22:25:59 +00001446 error = gfs2_permission(d_inode(odentry), MAY_WRITE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001447 if (error)
1448 goto out_gunlock;
1449 }
1450
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00001451 if (nip == NULL) {
1452 error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name, &da);
1453 if (error)
1454 goto out_gunlock;
1455 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001456
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00001457 if (da.nr_blocks) {
1458 struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
Abhi Dasb8fbf472015-03-18 12:03:41 -05001459 error = gfs2_quota_lock_check(ndip, &ap);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001460 if (error)
Bob Peterson5407e242012-05-18 09:28:23 -04001461 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001462
Steven Whitehouse7b9cff42013-10-02 11:13:25 +01001463 error = gfs2_inplace_reserve(ndip, &ap);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001464 if (error)
1465 goto out_gunlock_q;
1466
Steven Whitehouse534cf9c2014-01-06 12:03:05 +00001467 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(ndip, &da, 4) +
1468 4 * RES_LEAF + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001469 if (error)
1470 goto out_ipreserv;
1471 } else {
1472 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -05001473 5 * RES_LEAF + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001474 if (error)
1475 goto out_gunlock;
1476 }
1477
1478 /* Remove the target file, if it exists */
1479
Bob Peterson4327a9b2012-11-12 13:03:29 -05001480 if (nip)
1481 error = gfs2_unlink_inode(ndip, ndentry);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001482
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001483 error = update_moved_ino(ip, ndip, dir_rename);
1484 if (error)
1485 goto out_end_trans;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001486
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001487 error = gfs2_dir_del(odip, odentry);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001488 if (error)
1489 goto out_end_trans;
1490
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001491 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, &da);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001492 if (error)
1493 goto out_end_trans;
1494
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001495out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001496 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001497out_ipreserv:
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00001498 if (da.nr_blocks)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001499 gfs2_inplace_release(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001500out_gunlock_q:
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00001501 if (da.nr_blocks)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001502 gfs2_quota_unlock(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001503out_gunlock:
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001504 gfs2_dir_no_add(&da);
Bob Peterson72dbf472008-08-12 13:39:29 -05001505 while (x--) {
1506 gfs2_glock_dq(ghs + x);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001507 gfs2_holder_uninit(ghs + x);
Bob Peterson72dbf472008-08-12 13:39:29 -05001508 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001509out_gunlock_r:
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001510 if (r_gh.gh_gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001511 gfs2_glock_dq_uninit(&r_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001512out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001513 return error;
1514}
1515
1516/**
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001517 * 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
1671/**
Al Viro6b255392015-11-17 10:20:54 -05001672 * gfs2_get_link - Follow a symbolic link
David Teiglandb3b94fa2006-01-16 16:50:04 +00001673 * @dentry: The dentry of the link
Al Viro6b255392015-11-17 10:20:54 -05001674 * @inode: The inode of the link
Al Virofceef392015-12-29 15:58:39 -05001675 * @done: destructor for return value
David Teiglandb3b94fa2006-01-16 16:50:04 +00001676 *
Al Viroc177c2a2010-01-14 00:59:16 -05001677 * This can handle symlinks of any size.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001678 *
1679 * Returns: 0 on success or error code
1680 */
1681
Al Viro6b255392015-11-17 10:20:54 -05001682static const char *gfs2_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001683 struct inode *inode,
1684 struct delayed_call *done)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001685{
Al Viro6b255392015-11-17 10:20:54 -05001686 struct gfs2_inode *ip = GFS2_I(inode);
Al Viroc177c2a2010-01-14 00:59:16 -05001687 struct gfs2_holder i_gh;
1688 struct buffer_head *dibh;
Steven Whitehouse160b4022011-05-13 10:34:59 +01001689 unsigned int size;
Al Viroc177c2a2010-01-14 00:59:16 -05001690 char *buf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001691 int error;
1692
Al Viro6b255392015-11-17 10:20:54 -05001693 if (!dentry)
1694 return ERR_PTR(-ECHILD);
1695
Al Viroc177c2a2010-01-14 00:59:16 -05001696 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);
Al Viro680baac2015-05-02 13:32:22 -04001700 return ERR_PTR(error);
Al Viroc177c2a2010-01-14 00:59:16 -05001701 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001702
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001703 size = (unsigned int)i_size_read(&ip->i_inode);
1704 if (size == 0) {
Al Viroc177c2a2010-01-14 00:59:16 -05001705 gfs2_consist_inode(ip);
1706 buf = ERR_PTR(-EIO);
1707 goto out;
1708 }
1709
1710 error = gfs2_meta_inode_buffer(ip, &dibh);
1711 if (error) {
1712 buf = ERR_PTR(error);
1713 goto out;
1714 }
1715
Steven Whitehouse160b4022011-05-13 10:34:59 +01001716 buf = kzalloc(size + 1, GFP_NOFS);
Al Viroc177c2a2010-01-14 00:59:16 -05001717 if (!buf)
1718 buf = ERR_PTR(-ENOMEM);
1719 else
Steven Whitehouse160b4022011-05-13 10:34:59 +01001720 memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
Al Viroc177c2a2010-01-14 00:59:16 -05001721 brelse(dibh);
1722out:
1723 gfs2_glock_dq_uninit(&i_gh);
Al Viro680baac2015-05-02 13:32:22 -04001724 if (!IS_ERR(buf))
Al Virofceef392015-12-29 15:58:39 -05001725 set_delayed_call(done, kfree_link, buf);
Al Viro680baac2015-05-02 13:32:22 -04001726 return buf;
Al Viroc177c2a2010-01-14 00:59:16 -05001727}
1728
David Teiglandb3b94fa2006-01-16 16:50:04 +00001729/**
1730 * gfs2_permission -
Steven Whitehouse75d5cfb2011-01-19 09:42:40 +00001731 * @inode: The inode
1732 * @mask: The mask to be tested
1733 * @flags: Indicates whether this is an RCU path walk or not
David Teiglandb3b94fa2006-01-16 16:50:04 +00001734 *
Steven Whitehouse300c7d72006-11-27 09:55:28 -05001735 * 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 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001739 * Returns: errno
1740 */
1741
Al Viro10556cb2011-06-20 19:28:19 -04001742int gfs2_permission(struct inode *inode, int mask)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001743{
Nick Pigginb74c79e2011-01-07 17:49:58 +11001744 struct gfs2_inode *ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001745 struct gfs2_holder i_gh;
1746 int error;
Steven Whitehouse300c7d72006-11-27 09:55:28 -05001747 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001748
Nick Pigginb74c79e2011-01-07 17:49:58 +11001749
1750 ip = GFS2_I(inode);
Steven Whitehouse7afd88d2008-02-22 16:07:18 +00001751 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Benjamin Marzinski2e60d762014-11-13 20:42:04 -06001752 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;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001758 }
1759
Miklos Szeredif58ba882008-07-02 21:12:01 +02001760 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
1761 error = -EACCES;
1762 else
Al Viro2830ba72011-06-20 19:16:29 -04001763 error = generic_permission(inode, mask);
Steven Whitehouse300c7d72006-11-27 09:55:28 -05001764 if (unlock)
1765 gfs2_glock_dq_uninit(&i_gh);
1766
David Teiglandb3b94fa2006-01-16 16:50:04 +00001767 return error;
1768}
1769
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001770static int __gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
Steven Whitehouse194c0112011-05-09 14:06:38 +01001771{
Steven Whitehouse194c0112011-05-09 14:06:38 +01001772 setattr_copy(inode, attr);
1773 mark_inode_dirty(inode);
Steven Whitehouse194c0112011-05-09 14:06:38 +01001774 return 0;
1775}
1776
1777/**
1778 * gfs2_setattr_simple -
1779 * @ip:
1780 * @attr:
1781 *
1782 * Returns: errno
1783 */
1784
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001785int gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
Steven Whitehouse194c0112011-05-09 14:06:38 +01001786{
1787 int error;
1788
1789 if (current->journal_info)
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001790 return __gfs2_setattr_simple(inode, attr);
Steven Whitehouse194c0112011-05-09 14:06:38 +01001791
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001792 error = gfs2_trans_begin(GFS2_SB(inode), RES_DINODE, 0);
Steven Whitehouse194c0112011-05-09 14:06:38 +01001793 if (error)
1794 return error;
1795
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001796 error = __gfs2_setattr_simple(inode, attr);
1797 gfs2_trans_end(GFS2_SB(inode));
Steven Whitehouse194c0112011-05-09 14:06:38 +01001798 return error;
1799}
1800
David Teiglandb3b94fa2006-01-16 16:50:04 +00001801static int setattr_chown(struct inode *inode, struct iattr *attr)
1802{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001803 struct gfs2_inode *ip = GFS2_I(inode);
1804 struct gfs2_sbd *sdp = GFS2_SB(inode);
Eric W. Biederman7c06b5d2013-01-31 20:27:54 -08001805 kuid_t ouid, nuid;
1806 kgid_t ogid, ngid;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001807 int error;
Abhi Dasb8fbf472015-03-18 12:03:41 -05001808 struct gfs2_alloc_parms ap;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001809
Steven Whitehouse2933f922006-11-01 13:23:29 -05001810 ouid = inode->i_uid;
1811 ogid = inode->i_gid;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001812 nuid = attr->ia_uid;
1813 ngid = attr->ia_gid;
1814
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -08001815 if (!(attr->ia_valid & ATTR_UID) || uid_eq(ouid, nuid))
Eric W. Biedermanf4108a62013-01-31 17:49:26 -08001816 ouid = nuid = NO_UID_QUOTA_CHANGE;
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -08001817 if (!(attr->ia_valid & ATTR_GID) || gid_eq(ogid, ngid))
Eric W. Biedermanf4108a62013-01-31 17:49:26 -08001818 ogid = ngid = NO_GID_QUOTA_CHANGE;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001819
Bob Petersonb54e9a02015-10-26 10:40:28 -05001820 error = gfs2_rsqa_alloc(ip);
Bob Peterson62e96cf2014-01-06 17:16:01 -05001821 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
Abhi Dasb8fbf472015-03-18 12:03:41 -05001832 ap.target = gfs2_get_inode_blocks(&ip->i_inode);
1833
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -08001834 if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
1835 !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
Abhi Dasb8fbf472015-03-18 12:03:41 -05001836 error = gfs2_quota_check(ip, nuid, ngid, &ap);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001837 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
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001845 error = gfs2_setattr_simple(inode, attr);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001846 if (error)
1847 goto out_end_trans;
1848
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -08001849 if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
1850 !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
Abhi Das39a72582015-06-02 11:02:24 -05001851 gfs2_quota_change(ip, -(s64)ap.target, ouid, ogid);
Abhi Dasb8fbf472015-03-18 12:03:41 -05001852 gfs2_quota_change(ip, ap.target, nuid, ngid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001853 }
1854
Steven Whitehousea91ea692006-09-04 12:04:26 -04001855out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001856 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001857out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001858 gfs2_quota_unlock(ip);
Bob Peterson62e96cf2014-01-06 17:16:01 -05001859out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001860 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{
David Howells2b0143b2015-03-17 22:25:59 +00001876 struct inode *inode = d_inode(dentry);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001877 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001878 struct gfs2_holder i_gh;
1879 int error;
1880
Bob Petersonb54e9a02015-10-26 10:40:28 -05001881 error = gfs2_rsqa_alloc(ip);
Bob Peterson0a305e42012-06-06 11:17:59 +01001882 if (error)
1883 return error;
1884
David Teiglandb3b94fa2006-01-16 16:50:04 +00001885 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)
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001898 error = gfs2_setattr_size(inode, attr->ia_size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001899 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1900 error = setattr_chown(inode, attr);
Christoph Hellwige01580b2013-12-20 05:16:52 -08001901 else {
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001902 error = gfs2_setattr_simple(inode, attr);
Christoph Hellwige01580b2013-12-20 05:16:52 -08001903 if (!error && attr->ia_valid & ATTR_MODE)
1904 error = posix_acl_chmod(inode, inode->i_mode);
1905 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001906
Steven Whitehousea91ea692006-09-04 12:04:26 -04001907out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001908 if (!error)
1909 mark_inode_dirty(inode);
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001910 gfs2_glock_dq_uninit(&i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001911 return error;
1912}
1913
1914/**
1915 * gfs2_getattr - Read out an inode's attributes
Steven Whitehouse26c1a572006-09-04 15:32:10 -04001916 * @mnt: The vfsmount the inode is being accessed from
David Teiglandb3b94fa2006-01-16 16:50:04 +00001917 * @dentry: The dentry to stat
1918 * @stat: The inode's stats
1919 *
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001920 * 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 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001926 * Returns: errno
1927 */
1928
1929static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1930 struct kstat *stat)
1931{
David Howells2b0143b2015-03-17 22:25:59 +00001932 struct inode *inode = d_inode(dentry);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001933 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001934 struct gfs2_holder gh;
1935 int error;
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001936 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001937
Steven Whitehouse7afd88d2008-02-22 16:07:18 +00001938 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Benjamin Marzinski2e60d762014-11-13 20:42:04 -06001939 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1940 if (error)
1941 return error;
1942 unlock = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001943 }
1944
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001945 generic_fillattr(inode, stat);
Steven Whitehoused7c103d2007-01-25 17:14:59 +00001946 if (unlock)
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001947 gfs2_glock_dq_uninit(&gh);
1948
1949 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001950}
1951
1952static int gfs2_setxattr(struct dentry *dentry, const char *name,
1953 const void *data, size_t size, int flags)
1954{
David Howells2b0143b2015-03-17 22:25:59 +00001955 struct inode *inode = d_inode(dentry);
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001956 struct gfs2_inode *ip = GFS2_I(inode);
1957 struct gfs2_holder gh;
1958 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001959
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001960 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1961 ret = gfs2_glock_nq(&gh);
1962 if (ret == 0) {
Bob Petersonb54e9a02015-10-26 10:40:28 -05001963 ret = gfs2_rsqa_alloc(ip);
Steven Whitehouse645b2cc2012-07-26 14:15:17 +01001964 if (ret == 0)
1965 ret = generic_setxattr(dentry, name, data, size, flags);
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001966 gfs2_glock_dq(&gh);
1967 }
1968 gfs2_holder_uninit(&gh);
1969 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001970}
1971
1972static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1973 void *data, size_t size)
1974{
David Howells2b0143b2015-03-17 22:25:59 +00001975 struct inode *inode = d_inode(dentry);
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001976 struct gfs2_inode *ip = GFS2_I(inode);
1977 struct gfs2_holder gh;
1978 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001979
Steven Whitehouse7bd9ee52013-08-16 21:10:28 +01001980 /* For selinux during lookup */
1981 if (gfs2_glock_is_locked_by_me(ip->i_gl))
1982 return generic_getxattr(dentry, name, data, size);
1983
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001984 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;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001992}
1993
1994static int gfs2_removexattr(struct dentry *dentry, const char *name)
1995{
David Howells2b0143b2015-03-17 22:25:59 +00001996 struct inode *inode = d_inode(dentry);
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001997 struct gfs2_inode *ip = GFS2_I(inode);
1998 struct gfs2_holder gh;
1999 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002000
Steven Whitehouse40b78a32009-08-26 18:41:32 +01002001 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
2002 ret = gfs2_glock_nq(&gh);
2003 if (ret == 0) {
Bob Petersonb54e9a02015-10-26 10:40:28 -05002004 ret = gfs2_rsqa_alloc(ip);
Steven Whitehouse645b2cc2012-07-26 14:15:17 +01002005 if (ret == 0)
2006 ret = generic_removexattr(dentry, name);
Steven Whitehouse40b78a32009-08-26 18:41:32 +01002007 gfs2_glock_dq(&gh);
2008 }
2009 gfs2_holder_uninit(&gh);
2010 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002011}
2012
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002013static 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
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002055const struct inode_operations gfs2_file_iops = {
Al Viroe6305c42008-07-15 21:03:57 -04002056 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002057 .setattr = gfs2_setattr,
2058 .getattr = gfs2_getattr,
2059 .setxattr = gfs2_setxattr,
2060 .getxattr = gfs2_getxattr,
2061 .listxattr = gfs2_listxattr,
2062 .removexattr = gfs2_removexattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002063 .fiemap = gfs2_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02002064 .get_acl = gfs2_get_acl,
Christoph Hellwige01580b2013-12-20 05:16:52 -08002065 .set_acl = gfs2_set_acl,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002066};
2067
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002068const struct inode_operations gfs2_dir_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00002069 .create = gfs2_create,
2070 .lookup = gfs2_lookup,
2071 .link = gfs2_link,
2072 .unlink = gfs2_unlink,
2073 .symlink = gfs2_symlink,
2074 .mkdir = gfs2_mkdir,
Steven Whitehouse855d23c2011-05-09 16:42:37 +01002075 .rmdir = gfs2_unlink,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002076 .mknod = gfs2_mknod,
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05002077 .rename2 = gfs2_rename2,
Al Viroe6305c42008-07-15 21:03:57 -04002078 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002079 .setattr = gfs2_setattr,
2080 .getattr = gfs2_getattr,
2081 .setxattr = gfs2_setxattr,
2082 .getxattr = gfs2_getxattr,
2083 .listxattr = gfs2_listxattr,
2084 .removexattr = gfs2_removexattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002085 .fiemap = gfs2_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02002086 .get_acl = gfs2_get_acl,
Christoph Hellwige01580b2013-12-20 05:16:52 -08002087 .set_acl = gfs2_set_acl,
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01002088 .atomic_open = gfs2_atomic_open,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002089};
2090
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002091const struct inode_operations gfs2_symlink_iops = {
Al Viroc177c2a2010-01-14 00:59:16 -05002092 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -05002093 .get_link = gfs2_get_link,
Al Viroe6305c42008-07-15 21:03:57 -04002094 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002095 .setattr = gfs2_setattr,
2096 .getattr = gfs2_getattr,
2097 .setxattr = gfs2_setxattr,
2098 .getxattr = gfs2_getxattr,
2099 .listxattr = gfs2_listxattr,
2100 .removexattr = gfs2_removexattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002101 .fiemap = gfs2_fiemap,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002102};
2103