blob: ebff26ee6865bf356a69ef6bb8c5eaada8c649cf [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
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -050040static 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;
Steven Whitehouse194c0112011-05-09 14:06:38 +010054}
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
Steven Whitehouse194c0112011-05-09 14:06:38 +010092 * @type: The type of the inode
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -050093 * @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.
Steven Whitehouse194c0112011-05-09 14:06:38 +0100103 *
104 * Returns: A VFS inode, or an error
105 */
106
107struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500108 u64 no_addr, u64 no_formal_ino,
109 unsigned int blktype)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100110{
111 struct inode *inode;
112 struct gfs2_inode *ip;
113 struct gfs2_glock *io_gl = NULL;
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500114 struct gfs2_holder i_gh;
115 bool unlock = false;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100116 int error;
117
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500118 inode = gfs2_iget(sb, no_addr);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100119 if (!inode)
Steven Whitehouseac3beb62014-01-16 10:31:13 +0000120 return ERR_PTR(-ENOMEM);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100121
Bob Petersone97321f2016-04-12 16:14:26 -0400122 ip = GFS2_I(inode);
Bob Petersone97321f2016-04-12 16:14:26 -0400123
Steven Whitehouse194c0112011-05-09 14:06:38 +0100124 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
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500137 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
Steven Whitehouse194c0112011-05-09 14:06:38 +0100157 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))
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500160 goto fail_put;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100161
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
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500179 if (unlock)
180 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100181 return inode;
182
183fail_refresh:
Bob Petersona6a4d982013-05-29 11:51:52 -0400184 ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100185 ip->i_iopen_gh.gh_gl->gl_object = NULL;
Bob Peterson86d067a2015-12-07 15:10:42 -0600186 gfs2_glock_dq_wait(&ip->i_iopen_gh);
187 gfs2_holder_uninit(&ip->i_iopen_gh);
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500188fail_put:
Steven Whitehouse194c0112011-05-09 14:06:38 +0100189 if (io_gl)
190 gfs2_glock_put(io_gl);
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500191 if (unlock)
192 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100193 ip->i_gl->gl_object = NULL;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100194fail:
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;
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500203 struct inode *inode;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100204 int error;
205
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500206 inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, 0, blktype);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100207 if (IS_ERR(inode))
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500208 return inode;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100209
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;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100219 }
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500220 return inode;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100221
Steven Whitehouse194c0112011-05-09 14:06:38 +0100222fail_iput:
223 iput(inode);
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500224 return ERR_PTR(error);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100225}
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 &&
David Howells2b0143b2015-03-17 22:25:59 +0000276 dir == d_inode(sb->s_root))) {
Steven Whitehouse194c0112011-05-09 14:06:38 +0100277 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) {
Al Viro10556cb2011-06-20 19:28:19 -0400289 error = gfs2_permission(dir, MAY_EXEC);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100290 if (error)
291 goto out;
292 }
293
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +0100294 inode = gfs2_dir_search(dir, name, false);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100295 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,
Al Viro175a4eb2011-07-26 03:30:54 -0400315 umode_t mode)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100316{
317 int error;
318
Al Viro10556cb2011-06-20 19:28:19 -0400319 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100320 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
Steven Whitehouse194c0112011-05-09 14:06:38 +0100327 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
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000335static void munge_mode_uid_gid(const struct gfs2_inode *dip,
336 struct inode *inode)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100337{
338 if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -0800339 (dip->i_inode.i_mode & S_ISUID) &&
340 !uid_eq(dip->i_inode.i_uid, GLOBAL_ROOT_UID)) {
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000341 if (S_ISDIR(inode->i_mode))
342 inode->i_mode |= S_ISUID;
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -0800343 else if (!uid_eq(dip->i_inode.i_uid, current_fsuid()))
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000344 inode->i_mode &= ~07111;
345 inode->i_uid = dip->i_inode.i_uid;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100346 } else
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000347 inode->i_uid = current_fsuid();
Steven Whitehouse194c0112011-05-09 14:06:38 +0100348
349 if (dip->i_inode.i_mode & S_ISGID) {
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000350 if (S_ISDIR(inode->i_mode))
351 inode->i_mode |= S_ISGID;
352 inode->i_gid = dip->i_inode.i_gid;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100353 } else
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000354 inode->i_gid = current_fsgid();
Steven Whitehouse194c0112011-05-09 14:06:38 +0100355}
356
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000357static int alloc_dinode(struct gfs2_inode *ip, u32 flags, unsigned *dblocks)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100358{
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000359 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000360 struct gfs2_alloc_parms ap = { .target = *dblocks, .aflags = flags, };
Steven Whitehouse194c0112011-05-09 14:06:38 +0100361 int error;
362
Abhi Dasb8fbf472015-03-18 12:03:41 -0500363 error = gfs2_quota_lock_check(ip, &ap);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100364 if (error)
365 goto out;
366
Steven Whitehouse7b9cff42013-10-02 11:13:25 +0100367 error = gfs2_inplace_reserve(ip, &ap);
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000368 if (error)
369 goto out_quota;
370
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000371 error = gfs2_trans_begin(sdp, (*dblocks * RES_RG_BIT) + RES_STATFS + RES_QUOTA, 0);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100372 if (error)
373 goto out_ipreserv;
374
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000375 error = gfs2_alloc_blocks(ip, &ip->i_no_addr, dblocks, 1, &ip->i_generation);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000376 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;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100379
380 gfs2_trans_end(sdp);
381
382out_ipreserv:
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000383 gfs2_inplace_release(ip);
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000384out_quota:
385 gfs2_quota_unlock(ip);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100386out:
Steven Whitehouse194c0112011-05-09 14:06:38 +0100387 return error;
388}
389
Steven Whitehousef2741d92011-05-13 12:11:17 +0100390static void gfs2_init_dir(struct buffer_head *dibh,
391 const struct gfs2_inode *parent)
Steven Whitehousee2d0a132011-05-13 09:55:55 +0100392{
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
Steven Whitehouse194c0112011-05-09 14:06:38 +0100407/**
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000408 * 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
434/**
Steven Whitehouse194c0112011-05-09 14:06:38 +0100435 * init_dinode - Fill in a new dinode structure
Steven Whitehousef2741d92011-05-13 12:11:17 +0100436 * @dip: The directory this inode is being created in
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000437 * @ip: The inode
Steven Whitehousef2741d92011-05-13 12:11:17 +0100438 * @symname: The symlink destination (if a symlink)
Steven Whitehousef2741d92011-05-13 12:11:17 +0100439 * @bhp: The buffer head (returned to caller)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100440 *
441 */
442
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000443static void init_dinode(struct gfs2_inode *dip, struct gfs2_inode *ip,
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000444 const char *symname)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100445{
Steven Whitehouse194c0112011-05-09 14:06:38 +0100446 struct gfs2_dinode *di;
447 struct buffer_head *dibh;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100448
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000449 dibh = gfs2_meta_new(ip->i_gl, ip->i_no_addr);
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000450 gfs2_trans_add_meta(ip->i_gl, dibh);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100451 di = (struct gfs2_dinode *)dibh->b_data;
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000452 gfs2_dinode_out(ip, di);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100453
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000454 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));
Steven Whitehouse194c0112011-05-09 14:06:38 +0100456 di->__pad1 = 0;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100457 di->__pad2 = 0;
458 di->__pad3 = 0;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100459 memset(&di->__pad4, 0, sizeof(di->__pad4));
Steven Whitehouse194c0112011-05-09 14:06:38 +0100460 memset(&di->di_reserved, 0, sizeof(di->di_reserved));
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000461 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
Steven Whitehouse160b4022011-05-13 10:34:59 +0100462
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000463 switch(ip->i_inode.i_mode & S_IFMT) {
Steven Whitehouse160b4022011-05-13 10:34:59 +0100464 case S_IFDIR:
Steven Whitehousee2d0a132011-05-13 09:55:55 +0100465 gfs2_init_dir(dibh, dip);
Steven Whitehouse160b4022011-05-13 10:34:59 +0100466 break;
467 case S_IFLNK:
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000468 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, ip->i_inode.i_size);
Steven Whitehouse160b4022011-05-13 10:34:59 +0100469 break;
Steven Whitehousee2d0a132011-05-13 09:55:55 +0100470 }
471
Steven Whitehouse194c0112011-05-09 14:06:38 +0100472 set_buffer_uptodate(dibh);
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000473 brelse(dibh);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100474}
475
Steven Whitehouse534cf9c2014-01-06 12:03:05 +0000476/**
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
Steven Whitehouse194c0112011-05-09 14:06:38 +0100498static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000499 struct gfs2_inode *ip, struct gfs2_diradd *da)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100500{
501 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000502 struct gfs2_alloc_parms ap = { .target = da->nr_blocks, };
Steven Whitehouse194c0112011-05-09 14:06:38 +0100503 int error;
504
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000505 if (da->nr_blocks) {
Abhi Dasb8fbf472015-03-18 12:03:41 -0500506 error = gfs2_quota_lock_check(dip, &ap);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100507 if (error)
508 goto fail_quota_locks;
509
Steven Whitehouse7b9cff42013-10-02 11:13:25 +0100510 error = gfs2_inplace_reserve(dip, &ap);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100511 if (error)
512 goto fail_quota_locks;
513
Steven Whitehouse534cf9c2014-01-06 12:03:05 +0000514 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, da, 2), 0);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100515 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
Steven Whitehouse2b47dad2014-01-06 12:49:43 +0000523 error = gfs2_dir_add(&dip->i_inode, name, ip, da);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100524
Steven Whitehouse194c0112011-05-09 14:06:38 +0100525 gfs2_trans_end(sdp);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100526fail_ipreserv:
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000527 gfs2_inplace_release(dip);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100528fail_quota_locks:
529 gfs2_quota_unlock(dip);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100530 return error;
531}
532
H Hartley Sweeten46cc1e52011-09-23 15:51:32 -0700533static int gfs2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
Mimi Zohar9d8f13b2011-06-06 15:29:25 -0400534 void *fs_info)
535{
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;
545 }
546 return err;
547}
548
Steven Whitehouse194c0112011-05-09 14:06:38 +0100549/**
Steven Whitehousef2741d92011-05-13 12:11:17 +0100550 * gfs2_create_inode - Create a new inode
551 * @dir: The parent directory
552 * @dentry: The new dentry
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100553 * @file: If non-NULL, the file which is being opened
Steven Whitehousef2741d92011-05-13 12:11:17 +0100554 * @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)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100558 *
Steven Whitehousef2741d92011-05-13 12:11:17 +0100559 * Returns: 0 on success, or error code
Steven Whitehouse194c0112011-05-09 14:06:38 +0100560 */
561
Steven Whitehousef2741d92011-05-13 12:11:17 +0100562static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100563 struct file *file,
Al Viro175a4eb2011-07-26 03:30:54 -0400564 umode_t mode, dev_t dev, const char *symname,
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100565 unsigned int size, int excl, int *opened)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100566{
Steven Whitehousef2741d92011-05-13 12:11:17 +0100567 const struct qstr *name = &dentry->d_name;
Christoph Hellwige01580b2013-12-20 05:16:52 -0800568 struct posix_acl *default_acl, *acl;
Steven Whitehousef2741d92011-05-13 12:11:17 +0100569 struct gfs2_holder ghs[2];
Steven Whitehouse194c0112011-05-09 14:06:38 +0100570 struct inode *inode = NULL;
Bob Peterson8e2e0042012-07-19 08:12:40 -0400571 struct gfs2_inode *dip = GFS2_I(dir), *ip;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100572 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Bob Petersona4923862015-12-07 16:24:27 -0600573 struct gfs2_glock *io_gl = NULL;
Bob Peterson783013c2015-12-04 10:19:14 -0600574 int error, free_vfs_inode = 1;
Steven Whitehouse9dbe9612012-10-31 10:37:10 +0000575 u32 aflags = 0;
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000576 unsigned blocks = 1;
Bob Peterson19aeb5a2014-09-29 08:52:04 -0400577 struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, };
Steven Whitehouse194c0112011-05-09 14:06:38 +0100578
579 if (!name->len || name->len > GFS2_FNAMESIZE)
Steven Whitehousef2741d92011-05-13 12:11:17 +0100580 return -ENAMETOOLONG;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100581
Bob Petersonb54e9a02015-10-26 10:40:28 -0500582 error = gfs2_rsqa_alloc(dip);
Bob Peterson0a305e42012-06-06 11:17:59 +0100583 if (error)
584 return error;
585
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000586 error = gfs2_rindex_update(sdp);
587 if (error)
588 return error;
589
Steven Whitehousef2741d92011-05-13 12:11:17 +0100590 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100591 if (error)
592 goto fail;
593
594 error = create_ok(dip, name, mode);
595 if (error)
596 goto fail_gunlock;
597
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +0100598 inode = gfs2_dir_search(dir, &dentry->d_name, !S_ISREG(mode) || excl);
599 error = PTR_ERR(inode);
600 if (!IS_ERR(inode)) {
Al Viro571a4b52014-11-19 19:34:49 +0000601 if (S_ISDIR(inode->i_mode)) {
602 iput(inode);
603 inode = ERR_PTR(-EISDIR);
604 goto fail_gunlock;
605 }
Al Viro44bb31b2014-11-19 19:35:24 +0000606 d_instantiate(dentry, inode);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100607 error = 0;
Miklos Szeredi0d0d1102013-09-16 14:52:00 +0200608 if (file) {
Al Viro44bb31b2014-11-19 19:35:24 +0000609 if (S_ISREG(inode->i_mode))
Miklos Szeredi5ca1db42013-09-23 13:21:04 +0100610 error = finish_open(file, dentry, gfs2_open_common, opened);
Al Viro44bb31b2014-11-19 19:35:24 +0000611 else
612 error = finish_no_open(file, NULL);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100613 }
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +0100614 gfs2_glock_dq_uninit(ghs);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100615 return error;
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +0100616 } else if (error != -ENOENT) {
617 goto fail_gunlock;
618 }
619
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000620 error = gfs2_diradd_alloc_required(dir, name, &da);
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000621 if (error < 0)
622 goto fail_gunlock;
623
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000624 inode = new_inode(sdp->sd_vfs);
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000625 error = -ENOMEM;
626 if (!inode)
627 goto fail_gunlock;
628
Christoph Hellwige01580b2013-12-20 05:16:52 -0800629 error = posix_acl_create(dir, &mode, &default_acl, &acl);
630 if (error)
Bob Peterson783013c2015-12-04 10:19:14 -0600631 goto fail_gunlock;
Christoph Hellwige01580b2013-12-20 05:16:52 -0800632
Bob Peterson8e2e0042012-07-19 08:12:40 -0400633 ip = GFS2_I(inode);
Bob Petersonb54e9a02015-10-26 10:40:28 -0500634 error = gfs2_rsqa_alloc(ip);
Steven Whitehouseff7f4cb2012-09-10 10:03:50 +0100635 if (error)
Christoph Hellwige01580b2013-12-20 05:16:52 -0800636 goto fail_free_acls;
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000637
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000638 inode->i_mode = mode;
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000639 set_nlink(inode, S_ISDIR(mode) ? 2 : 1);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000640 inode->i_rdev = dev;
641 inode->i_size = size;
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000642 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Steven Whitehouse28fb3022013-02-26 19:09:35 +0000643 gfs2_set_inode_blocks(inode, 1);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000644 munge_mode_uid_gid(dip, inode);
Abhi Das00a158b2014-09-18 21:40:28 -0500645 check_and_update_goal(dip);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000646 ip->i_goal = dip->i_goal;
Steven Whitehouse28fb3022013-02-26 19:09:35 +0000647 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 }
Abhi Dasacc546f2015-11-10 15:07:26 -0600666
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
Steven Whitehouse28fb3022013-02-26 19:09:35 +0000671 gfs2_set_inode_flags(inode);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000672
David Howells2b0143b2015-03-17 22:25:59 +0000673 if ((GFS2_I(d_inode(sdp->sd_root_dir)) == dip) ||
Steven Whitehouse9dbe9612012-10-31 10:37:10 +0000674 (dip->i_diskflags & GFS2_DIF_TOPDIR))
675 aflags |= GFS2_AF_ORLOV;
676
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000677 if (default_acl || acl)
678 blocks++;
679
680 error = alloc_dinode(ip, aflags, &blocks);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000681 if (error)
682 goto fail_free_inode;
683
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000684 gfs2_set_inode_blocks(inode, blocks);
685
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000686 error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
687 if (error)
688 goto fail_free_inode;
689
Bob Peterson1e2d9d42012-11-21 09:56:00 -0500690 ip->i_gl->gl_object = ip;
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000691 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
692 if (error)
693 goto fail_free_inode;
694
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000695 error = gfs2_trans_begin(sdp, blocks, 0);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000696 if (error)
Steven Whitehouseff7f4cb2012-09-10 10:03:50 +0100697 goto fail_gunlock2;
Bob Peterson0a305e42012-06-06 11:17:59 +0100698
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000699 if (blocks > 1) {
700 ip->i_eattr = ip->i_no_addr + 1;
701 gfs2_init_xattr(ip);
702 }
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000703 init_dinode(dip, ip, symname);
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000704 gfs2_trans_end(sdp);
705
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000706 error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
707 if (error)
708 goto fail_gunlock2;
709
Bob Petersona4923862015-12-07 16:24:27 -0600710 BUG_ON(test_and_set_bit(GLF_INODE_CREATING, &io_gl->gl_flags));
711
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000712 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
713 if (error)
714 goto fail_gunlock2;
715
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
Bob Peterson783013c2015-12-04 10:19:14 -0600721 free_vfs_inode = 0; /* After this point, the inode is no longer
722 considered free. Any failures need to undo
723 the gfs2 structures. */
Christoph Hellwige01580b2013-12-20 05:16:52 -0800724 if (default_acl) {
Al Viro1a39ba92016-05-13 03:59:17 +0200725 error = __gfs2_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
Christoph Hellwige01580b2013-12-20 05:16:52 -0800726 posix_acl_release(default_acl);
727 }
728 if (acl) {
729 if (!error)
Al Viro1a39ba92016-05-13 03:59:17 +0200730 error = __gfs2_set_acl(inode, acl, ACL_TYPE_ACCESS);
Christoph Hellwige01580b2013-12-20 05:16:52 -0800731 posix_acl_release(acl);
732 }
733
Steven Whitehouse194c0112011-05-09 14:06:38 +0100734 if (error)
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000735 goto fail_gunlock3;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100736
Bob Petersonf45dc262014-03-19 09:37:00 -0400737 error = security_inode_init_security(&ip->i_inode, &dip->i_inode, name,
738 &gfs2_initxattrs, NULL);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100739 if (error)
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000740 goto fail_gunlock3;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100741
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000742 error = link_dinode(dip, name, ip, &da);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100743 if (error)
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000744 goto fail_gunlock3;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100745
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000746 mark_inode_dirty(inode);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100747 d_instantiate(dentry, inode);
Miklos Szeredic5bf8fe2013-09-16 14:52:03 +0200748 if (file) {
749 *opened |= FILE_CREATED;
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100750 error = finish_open(file, dentry, gfs2_open_common, opened);
Miklos Szeredic5bf8fe2013-09-16 14:52:03 +0200751 }
Steven Whitehouse28fb3022013-02-26 19:09:35 +0000752 gfs2_glock_dq_uninit(ghs);
753 gfs2_glock_dq_uninit(ghs + 1);
Bob Petersona4923862015-12-07 16:24:27 -0600754 clear_bit(GLF_INODE_CREATING, &io_gl->gl_flags);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100755 return error;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100756
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000757fail_gunlock3:
Bob Peterson783013c2015-12-04 10:19:14 -0600758 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
759 gfs2_glock_put(io_gl);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100760fail_gunlock2:
Bob Petersona4923862015-12-07 16:24:27 -0600761 if (io_gl)
762 clear_bit(GLF_INODE_CREATING, &io_gl->gl_flags);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100763 gfs2_glock_dq_uninit(ghs + 1);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000764fail_free_inode:
765 if (ip->i_gl)
766 gfs2_glock_put(ip->i_gl);
Bob Petersonb54e9a02015-10-26 10:40:28 -0500767 gfs2_rsqa_delete(ip, NULL);
Christoph Hellwige01580b2013-12-20 05:16:52 -0800768fail_free_acls:
769 if (default_acl)
770 posix_acl_release(default_acl);
771 if (acl)
772 posix_acl_release(acl);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100773fail_gunlock:
Steven Whitehouse2b47dad2014-01-06 12:49:43 +0000774 gfs2_dir_no_add(&da);
Steven Whitehousef2741d92011-05-13 12:11:17 +0100775 gfs2_glock_dq_uninit(ghs);
Steven Whitehouse40ac2182011-08-02 13:17:27 +0100776 if (inode && !IS_ERR(inode)) {
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000777 clear_nlink(inode);
Abhi Das05978802014-03-31 10:33:17 -0500778 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);
Steven Whitehouse40ac2182011-08-02 13:17:27 +0100782 iput(inode);
783 }
Steven Whitehouse194c0112011-05-09 14:06:38 +0100784fail:
Steven Whitehousef2741d92011-05-13 12:11:17 +0100785 return error;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100786}
Steven Whitehousef2741d92011-05-13 12:11:17 +0100787
David Teiglandb3b94fa2006-01-16 16:50:04 +0000788/**
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,
Al Viroebfc3b42012-06-10 18:05:36 -0400798 umode_t mode, bool excl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000799{
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100800 return gfs2_create_inode(dir, dentry, NULL, S_IFREG | mode, 0, NULL, 0, excl, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000801}
802
803/**
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100804 * __gfs2_lookup - Look up a filename in a directory and return its inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000805 * @dir: The directory inode
806 * @dentry: The dentry of the new inode
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100807 * @file: File to be opened
808 * @opened: atomic_open flags
David Teiglandb3b94fa2006-01-16 16:50:04 +0000809 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000810 *
811 * Returns: errno
812 */
813
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100814static struct dentry *__gfs2_lookup(struct inode *dir, struct dentry *dentry,
815 struct file *file, int *opened)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000816{
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100817 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);
Benjamin Coddington7b7a9112014-09-10 14:09:20 -0400824 if (inode == NULL) {
825 d_add(dentry, NULL);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100826 return NULL;
Benjamin Coddington7b7a9112014-09-10 14:09:20 -0400827 }
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100828 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);
Steven Whitehouse9656b2c2008-01-08 08:14:30 +0000836 }
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100837
838 d = d_splice_alias(inode, dentry);
J. Bruce Fieldsd57b9c92014-01-16 13:51:07 -0500839 if (IS_ERR(d)) {
J. Bruce Fieldsd57b9c92014-01-16 13:51:07 -0500840 gfs2_glock_dq_uninit(&gh);
841 return d;
842 }
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100843 if (file && S_ISREG(inode->i_mode))
844 error = finish_open(file, dentry, gfs2_open_common, opened);
845
846 gfs2_glock_dq_uninit(&gh);
Miklos Szeredi5ca1db42013-09-23 13:21:04 +0100847 if (error) {
848 dput(d);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100849 return ERR_PTR(error);
Miklos Szeredi5ca1db42013-09-23 13:21:04 +0100850 }
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100851 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);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000858}
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{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400875 struct gfs2_inode *dip = GFS2_I(dir);
876 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Howells2b0143b2015-03-17 22:25:59 +0000877 struct inode *inode = d_inode(old_dentry);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400878 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000879 struct gfs2_holder ghs[2];
Steven Whitehouse2baee032011-05-09 12:08:36 +0100880 struct buffer_head *dibh;
Bob Peterson19aeb5a2014-09-29 08:52:04 -0400881 struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000882 int error;
883
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500884 if (S_ISDIR(inode->i_mode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000885 return -EPERM;
886
Bob Petersonb54e9a02015-10-26 10:40:28 -0500887 error = gfs2_rsqa_alloc(dip);
Bob Peterson0a305e42012-06-06 11:17:59 +0100888 if (error)
889 return error;
890
David Teiglandb3b94fa2006-01-16 16:50:04 +0000891 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
Bob Peterson72dbf472008-08-12 13:39:29 -0500894 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000895 if (error)
Bob Peterson72dbf472008-08-12 13:39:29 -0500896 goto out_parent;
897
898 error = gfs2_glock_nq(ghs + 1); /* child */
899 if (error)
900 goto out_child;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000901
Steven Whitehoused192a8e2011-05-05 12:35:40 +0100902 error = -ENOENT;
903 if (inode->i_nlink == 0)
904 goto out_gunlock;
905
Al Viro10556cb2011-06-20 19:28:19 -0400906 error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000907 if (error)
908 goto out_gunlock;
909
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100910 error = gfs2_dir_check(dir, &dentry->d_name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000911 switch (error) {
912 case -ENOENT:
913 break;
914 case 0:
915 error = -EEXIST;
916 default:
917 goto out_gunlock;
918 }
919
920 error = -EINVAL;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500921 if (!dip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000922 goto out_gunlock;
923 error = -EFBIG;
Steven Whitehousead6203f2008-11-03 13:59:19 +0000924 if (dip->i_entries == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000925 goto out_gunlock;
926 error = -EPERM;
927 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
928 goto out_gunlock;
929 error = -EINVAL;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500930 if (!ip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000931 goto out_gunlock;
932 error = -EMLINK;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500933 if (ip->i_inode.i_nlink == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000934 goto out_gunlock;
935
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000936 error = gfs2_diradd_alloc_required(dir, &dentry->d_name, &da);
Steven Whitehousec7526662006-03-20 12:30:04 -0500937 if (error < 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000938 goto out_gunlock;
939
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000940 if (da.nr_blocks) {
941 struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
Abhi Dasb8fbf472015-03-18 12:03:41 -0500942 error = gfs2_quota_lock_check(dip, &ap);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000943 if (error)
Bob Peterson5407e242012-05-18 09:28:23 -0400944 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000945
Steven Whitehouse7b9cff42013-10-02 11:13:25 +0100946 error = gfs2_inplace_reserve(dip, &ap);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000947 if (error)
948 goto out_gunlock_q;
949
Steven Whitehouse534cf9c2014-01-06 12:03:05 +0000950 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, &da, 2), 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000951 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
Steven Whitehouse2baee032011-05-09 12:08:36 +0100959 error = gfs2_meta_inode_buffer(ip, &dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000960 if (error)
961 goto out_end_trans;
962
Steven Whitehouse2b47dad2014-01-06 12:49:43 +0000963 error = gfs2_dir_add(dir, &dentry->d_name, ip, &da);
Steven Whitehouse2baee032011-05-09 12:08:36 +0100964 if (error)
965 goto out_brelse;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000966
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000967 gfs2_trans_add_meta(ip->i_gl, dibh);
Steven Whitehouse2baee032011-05-09 12:08:36 +0100968 inc_nlink(&ip->i_inode);
969 ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100970 ihold(inode);
971 d_instantiate(dentry, inode);
972 mark_inode_dirty(inode);
Steven Whitehouse2baee032011-05-09 12:08:36 +0100973
974out_brelse:
975 brelse(dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400976out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000977 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400978out_ipres:
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000979 if (da.nr_blocks)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000980 gfs2_inplace_release(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400981out_gunlock_q:
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000982 if (da.nr_blocks)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000983 gfs2_quota_unlock(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400984out_gunlock:
Steven Whitehouse2b47dad2014-01-06 12:49:43 +0000985 gfs2_dir_no_add(&da);
Bob Peterson72dbf472008-08-12 13:39:29 -0500986 gfs2_glock_dq(ghs + 1);
987out_child:
988 gfs2_glock_dq(ghs);
989out_parent:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000990 gfs2_holder_uninit(ghs);
991 gfs2_holder_uninit(ghs + 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000992 return error;
993}
994
Steven Whitehouse87ec2172009-05-22 10:54:50 +0100995/*
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) &&
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -08001015 !uid_eq(dip->i_inode.i_uid, current_fsuid()) &&
1016 !uid_eq(ip->i_inode.i_uid, current_fsuid()) && !capable(CAP_FOWNER))
Steven Whitehouse87ec2172009-05-22 10:54:50 +01001017 return -EPERM;
1018
1019 if (IS_APPEND(&dip->i_inode))
1020 return -EPERM;
1021
Al Viro10556cb2011-06-20 19:28:19 -04001022 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
Steven Whitehouse87ec2172009-05-22 10:54:50 +01001023 if (error)
1024 return error;
1025
Fabian Frederick37975f12014-10-09 22:49:21 +02001026 return gfs2_dir_check(&dip->i_inode, name, ip);
Steven Whitehouse87ec2172009-05-22 10:54:50 +01001027}
1028
David Teiglandb3b94fa2006-01-16 16:50:04 +00001029/**
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001030 * 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
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001033 * @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,
Bob Peterson4327a9b2012-11-12 13:03:29 -05001042 const struct dentry *dentry)
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001043{
David Howells2b0143b2015-03-17 22:25:59 +00001044 struct inode *inode = d_inode(dentry);
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001045 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);
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001058 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
David Teiglandb3b94fa2006-01-16 16:50:04 +00001068 * @dentry: The file itself
1069 *
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001070 * This routine uses the type of the inode as a flag to figure out
1071 * whether this is an unlink or an rmdir.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001072 *
1073 * Returns: errno
1074 */
1075
1076static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
1077{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001078 struct gfs2_inode *dip = GFS2_I(dir);
1079 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Howells2b0143b2015-03-17 22:25:59 +00001080 struct inode *inode = d_inode(dentry);
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001081 struct gfs2_inode *ip = GFS2_I(inode);
Russell Cattelanddee7602007-01-29 17:13:44 -06001082 struct gfs2_holder ghs[3];
1083 struct gfs2_rgrpd *rgd;
Bob Peterson5e2f7d62012-04-04 22:11:16 -04001084 int error;
1085
1086 error = gfs2_rindex_update(sdp);
1087 if (error)
1088 return error;
1089
1090 error = -EROFS;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001091
Russell Cattelanddee7602007-01-29 17:13:44 -06001092 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
1093 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
1094
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001095 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1);
Steven Whitehousea365fbf2012-02-24 15:09:14 +00001096 if (!rgd)
Steven Whitehouse87654892011-11-08 14:04:20 +00001097 goto out_inodes;
Steven Whitehousea365fbf2012-02-24 15:09:14 +00001098
Russell Cattelanddee7602007-01-29 17:13:44 -06001099 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
1100
1101
Steven Whitehouse8497a462007-08-26 14:23:56 +01001102 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001103 if (error)
Steven Whitehouse8497a462007-08-26 14:23:56 +01001104 goto out_parent;
1105
1106 error = gfs2_glock_nq(ghs + 1); /* child */
1107 if (error)
1108 goto out_child;
1109
Steven Whitehoused192a8e2011-05-05 12:35:40 +01001110 error = -ENOENT;
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001111 if (inode->i_nlink == 0)
Steven Whitehoused192a8e2011-05-05 12:35:40 +01001112 goto out_rgrp;
1113
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001114 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
Steven Whitehouse8497a462007-08-26 14:23:56 +01001120 error = gfs2_glock_nq(ghs + 2); /* rgrp */
1121 if (error)
1122 goto out_rgrp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001123
1124 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
1125 if (error)
Bob Peterson72dbf472008-08-12 13:39:29 -05001126 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001127
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001128 error = gfs2_trans_begin(sdp, 2*RES_DINODE + 3*RES_LEAF + RES_RG_BIT, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001129 if (error)
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001130 goto out_end_trans;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001131
Bob Peterson4327a9b2012-11-12 13:03:29 -05001132 error = gfs2_unlink_inode(dip, dentry);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001133
1134out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001135 gfs2_trans_end(sdp);
Bob Peterson72dbf472008-08-12 13:39:29 -05001136out_gunlock:
Steven Whitehouse8497a462007-08-26 14:23:56 +01001137 gfs2_glock_dq(ghs + 2);
1138out_rgrp:
Steven Whitehouse8497a462007-08-26 14:23:56 +01001139 gfs2_glock_dq(ghs + 1);
1140out_child:
Steven Whitehouse8497a462007-08-26 14:23:56 +01001141 gfs2_glock_dq(ghs);
1142out_parent:
Steven Whitehouse87654892011-11-08 14:04:20 +00001143 gfs2_holder_uninit(ghs + 2);
1144out_inodes:
1145 gfs2_holder_uninit(ghs + 1);
Steven Whitehouse8497a462007-08-26 14:23:56 +01001146 gfs2_holder_uninit(ghs);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001147 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{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001162 struct gfs2_sbd *sdp = GFS2_SB(dir);
Steven Whitehouse160b4022011-05-13 10:34:59 +01001163 unsigned int size;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001164
David Teiglandb3b94fa2006-01-16 16:50:04 +00001165 size = strlen(symname);
1166 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
1167 return -ENAMETOOLONG;
1168
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001169 return gfs2_create_inode(dir, dentry, NULL, S_IFLNK | S_IRWXUGO, 0, symname, size, 0, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001170}
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
Al Viro18bb1db2011-07-26 01:41:39 -04001181static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001182{
Steven Whitehouse28fb3022013-02-26 19:09:35 +00001183 struct gfs2_sbd *sdp = GFS2_SB(dir);
1184 unsigned dsize = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001185 return gfs2_create_inode(dir, dentry, NULL, S_IFDIR | mode, 0, NULL, dsize, 0, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001186}
1187
1188/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001189 * 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
Steven Whitehousef2741d92011-05-13 12:11:17 +01001193 * @dev: The device specification of the special file
David Teiglandb3b94fa2006-01-16 16:50:04 +00001194 *
1195 */
1196
Al Viro1a67aaf2011-07-26 01:52:52 -04001197static int gfs2_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001198 dev_t dev)
1199{
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001200 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,
Antonio Ospite86fbca42015-05-01 12:54:38 -05001216 struct file *file, unsigned flags,
1217 umode_t mode, int *opened)
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001218{
1219 struct dentry *d;
1220 bool excl = !!(flags & O_EXCL);
1221
Al Viro4d93bc32014-09-12 18:21:05 -04001222 if (!d_unhashed(dentry))
1223 goto skip_lookup;
1224
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001225 d = __gfs2_lookup(dir, dentry, file, opened);
1226 if (IS_ERR(d))
1227 return PTR_ERR(d);
Miklos Szeredi5ca1db42013-09-23 13:21:04 +01001228 if (d != NULL)
1229 dentry = d;
David Howells2b0143b2015-03-17 22:25:59 +00001230 if (d_really_is_positive(dentry)) {
Al Viroec7d8792014-11-19 19:35:58 +00001231 if (!(*opened & FILE_OPENED))
1232 return finish_no_open(file, d);
Miklos Szeredi5ca1db42013-09-23 13:21:04 +01001233 dput(d);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001234 return 0;
1235 }
1236
Miklos Szeredi5ca1db42013-09-23 13:21:04 +01001237 BUG_ON(d != NULL);
Al Viro4d93bc32014-09-12 18:21:05 -04001238
1239skip_lookup:
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001240 if (!(flags & O_CREAT))
1241 return -ENOENT;
1242
1243 return gfs2_create_inode(dir, dentry, file, S_IFREG | mode, 0, NULL, 0, excl, opened);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001244}
1245
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001246/*
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;
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001262 int error = 0;
1263
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001264 igrab(dir);
1265
1266 for (;;) {
1267 if (dir == &this->i_inode) {
1268 error = -EINVAL;
1269 break;
1270 }
David Howells2b0143b2015-03-17 22:25:59 +00001271 if (dir == d_inode(sb->s_root)) {
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001272 error = 0;
1273 break;
1274 }
1275
Steven Whitehouse8d123582010-09-17 12:30:23 +01001276 tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
Abhi Das48f8f712014-03-12 03:41:44 -05001277 if (!tmp) {
1278 error = -ENOENT;
1279 break;
1280 }
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001281 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
David Teiglandb3b94fa2006-01-16 16:50:04 +00001295/**
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001296 * 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
1324/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001325 * 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{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001337 struct gfs2_inode *odip = GFS2_I(odir);
1338 struct gfs2_inode *ndip = GFS2_I(ndir);
David Howells2b0143b2015-03-17 22:25:59 +00001339 struct gfs2_inode *ip = GFS2_I(d_inode(odentry));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001340 struct gfs2_inode *nip = NULL;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001341 struct gfs2_sbd *sdp = GFS2_SB(odir);
Steven Whitehouse8339ee52011-08-31 16:38:29 +01001342 struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, };
Russell Cattelanddee7602007-01-29 17:13:44 -06001343 struct gfs2_rgrpd *nrgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001344 unsigned int num_gh;
1345 int dir_rename = 0;
Bob Peterson19aeb5a2014-09-29 08:52:04 -04001346 struct gfs2_diradd da = { .nr_blocks = 0, .save_loc = 0, };
David Teiglandb3b94fa2006-01-16 16:50:04 +00001347 unsigned int x;
1348 int error;
1349
David Howells2b0143b2015-03-17 22:25:59 +00001350 if (d_really_is_positive(ndentry)) {
1351 nip = GFS2_I(d_inode(ndentry));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001352 if (ip == nip)
1353 return 0;
1354 }
1355
Bob Peterson5e2f7d62012-04-04 22:11:16 -04001356 error = gfs2_rindex_update(sdp);
1357 if (error)
1358 return error;
1359
Bob Petersonb54e9a02015-10-26 10:40:28 -05001360 error = gfs2_rsqa_alloc(ndip);
Bob Peterson0a305e42012-06-06 11:17:59 +01001361 if (error)
1362 return error;
1363
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001364 if (odip != ndip) {
1365 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
1366 0, &r_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001367 if (error)
1368 goto out;
1369
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001370 if (S_ISDIR(ip->i_inode.i_mode)) {
1371 dir_rename = 1;
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001372 /* don't move a directory into its subdir */
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001373 error = gfs2_ok_to_move(ip, ndip);
1374 if (error)
1375 goto out_gunlock_r;
1376 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001377 }
1378
Steven Whitehoused9d1ca32006-06-21 15:38:17 -04001379 num_gh = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001380 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -04001381 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++;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001387
Steven Whitehoused9d1ca32006-06-21 15:38:17 -04001388 if (nip) {
1389 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
1390 num_gh++;
Russell Cattelanddee7602007-01-29 17:13:44 -06001391 /* 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 */
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001395 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr, 1);
Russell Cattelanddee7602007-01-29 17:13:44 -06001396 if (nrgd)
1397 gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -04001398 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001399
Bob Peterson72dbf472008-08-12 13:39:29 -05001400 for (x = 0; x < num_gh; x++) {
1401 error = gfs2_glock_nq(ghs + x);
1402 if (error)
1403 goto out_gunlock;
1404 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001405
Steven Whitehoused192a8e2011-05-05 12:35:40 +01001406 error = -ENOENT;
1407 if (ip->i_inode.i_nlink == 0)
1408 goto out_gunlock;
1409
David Teiglandb3b94fa2006-01-16 16:50:04 +00001410 /* 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
Steven Whitehoused192a8e2011-05-05 12:35:40 +01001423 if (nip->i_inode.i_nlink == 0) {
1424 error = -EAGAIN;
1425 goto out_gunlock;
1426 }
1427
Steven Whitehouseb60623c2006-11-01 12:22:46 -05001428 if (S_ISDIR(nip->i_inode.i_mode)) {
Steven Whitehousead6203f2008-11-03 13:59:19 +00001429 if (nip->i_entries < 2) {
Steven Whitehouse94fb7632011-05-09 13:36:10 +01001430 gfs2_consist_inode(nip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001431 error = -EIO;
1432 goto out_gunlock;
1433 }
Steven Whitehousead6203f2008-11-03 13:59:19 +00001434 if (nip->i_entries > 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001435 error = -ENOTEMPTY;
1436 goto out_gunlock;
1437 }
1438 }
1439 } else {
Al Viro10556cb2011-06-20 19:28:19 -04001440 error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001441 if (error)
1442 goto out_gunlock;
1443
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001444 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001445 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) {
Steven Whitehouse4f561102006-11-01 14:04:17 -05001456 if (!ndip->i_inode.i_nlink) {
Steven Whitehoused192a8e2011-05-05 12:35:40 +01001457 error = -ENOENT;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001458 goto out_gunlock;
1459 }
Steven Whitehousead6203f2008-11-03 13:59:19 +00001460 if (ndip->i_entries == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001461 error = -EFBIG;
1462 goto out_gunlock;
1463 }
Steven Whitehouseb60623c2006-11-01 12:22:46 -05001464 if (S_ISDIR(ip->i_inode.i_mode) &&
Steven Whitehouse4f561102006-11-01 14:04:17 -05001465 ndip->i_inode.i_nlink == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001466 error = -EMLINK;
1467 goto out_gunlock;
1468 }
1469 }
1470 }
1471
1472 /* Check out the dir to be renamed */
1473
1474 if (dir_rename) {
David Howells2b0143b2015-03-17 22:25:59 +00001475 error = gfs2_permission(d_inode(odentry), MAY_WRITE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001476 if (error)
1477 goto out_gunlock;
1478 }
1479
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00001480 if (nip == NULL) {
1481 error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name, &da);
1482 if (error)
1483 goto out_gunlock;
1484 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001485
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00001486 if (da.nr_blocks) {
1487 struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
Abhi Dasb8fbf472015-03-18 12:03:41 -05001488 error = gfs2_quota_lock_check(ndip, &ap);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001489 if (error)
Bob Peterson5407e242012-05-18 09:28:23 -04001490 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001491
Steven Whitehouse7b9cff42013-10-02 11:13:25 +01001492 error = gfs2_inplace_reserve(ndip, &ap);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001493 if (error)
1494 goto out_gunlock_q;
1495
Steven Whitehouse534cf9c2014-01-06 12:03:05 +00001496 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(ndip, &da, 4) +
1497 4 * RES_LEAF + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001498 if (error)
1499 goto out_ipreserv;
1500 } else {
1501 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -05001502 5 * RES_LEAF + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001503 if (error)
1504 goto out_gunlock;
1505 }
1506
1507 /* Remove the target file, if it exists */
1508
Bob Peterson4327a9b2012-11-12 13:03:29 -05001509 if (nip)
1510 error = gfs2_unlink_inode(ndip, ndentry);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001511
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001512 error = update_moved_ino(ip, ndip, dir_rename);
1513 if (error)
1514 goto out_end_trans;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001515
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001516 error = gfs2_dir_del(odip, odentry);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001517 if (error)
1518 goto out_end_trans;
1519
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001520 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, &da);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001521 if (error)
1522 goto out_end_trans;
1523
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001524out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001525 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001526out_ipreserv:
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00001527 if (da.nr_blocks)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001528 gfs2_inplace_release(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001529out_gunlock_q:
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00001530 if (da.nr_blocks)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001531 gfs2_quota_unlock(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001532out_gunlock:
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001533 gfs2_dir_no_add(&da);
Bob Peterson72dbf472008-08-12 13:39:29 -05001534 while (x--) {
1535 gfs2_glock_dq(ghs + x);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001536 gfs2_holder_uninit(ghs + x);
Bob Peterson72dbf472008-08-12 13:39:29 -05001537 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001538out_gunlock_r:
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001539 if (r_gh.gh_gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001540 gfs2_glock_dq_uninit(&r_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001541out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001542 return error;
1543}
1544
1545/**
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001546 * 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
1700/**
Al Viro6b255392015-11-17 10:20:54 -05001701 * gfs2_get_link - Follow a symbolic link
David Teiglandb3b94fa2006-01-16 16:50:04 +00001702 * @dentry: The dentry of the link
Al Viro6b255392015-11-17 10:20:54 -05001703 * @inode: The inode of the link
Al Virofceef392015-12-29 15:58:39 -05001704 * @done: destructor for return value
David Teiglandb3b94fa2006-01-16 16:50:04 +00001705 *
Al Viroc177c2a2010-01-14 00:59:16 -05001706 * This can handle symlinks of any size.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001707 *
1708 * Returns: 0 on success or error code
1709 */
1710
Al Viro6b255392015-11-17 10:20:54 -05001711static const char *gfs2_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001712 struct inode *inode,
1713 struct delayed_call *done)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001714{
Al Viro6b255392015-11-17 10:20:54 -05001715 struct gfs2_inode *ip = GFS2_I(inode);
Al Viroc177c2a2010-01-14 00:59:16 -05001716 struct gfs2_holder i_gh;
1717 struct buffer_head *dibh;
Steven Whitehouse160b4022011-05-13 10:34:59 +01001718 unsigned int size;
Al Viroc177c2a2010-01-14 00:59:16 -05001719 char *buf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001720 int error;
1721
Al Viro6b255392015-11-17 10:20:54 -05001722 if (!dentry)
1723 return ERR_PTR(-ECHILD);
1724
Al Viroc177c2a2010-01-14 00:59:16 -05001725 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);
Al Viro680baac2015-05-02 13:32:22 -04001729 return ERR_PTR(error);
Al Viroc177c2a2010-01-14 00:59:16 -05001730 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001731
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001732 size = (unsigned int)i_size_read(&ip->i_inode);
1733 if (size == 0) {
Al Viroc177c2a2010-01-14 00:59:16 -05001734 gfs2_consist_inode(ip);
1735 buf = ERR_PTR(-EIO);
1736 goto out;
1737 }
1738
1739 error = gfs2_meta_inode_buffer(ip, &dibh);
1740 if (error) {
1741 buf = ERR_PTR(error);
1742 goto out;
1743 }
1744
Steven Whitehouse160b4022011-05-13 10:34:59 +01001745 buf = kzalloc(size + 1, GFP_NOFS);
Al Viroc177c2a2010-01-14 00:59:16 -05001746 if (!buf)
1747 buf = ERR_PTR(-ENOMEM);
1748 else
Steven Whitehouse160b4022011-05-13 10:34:59 +01001749 memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
Al Viroc177c2a2010-01-14 00:59:16 -05001750 brelse(dibh);
1751out:
1752 gfs2_glock_dq_uninit(&i_gh);
Al Viro680baac2015-05-02 13:32:22 -04001753 if (!IS_ERR(buf))
Al Virofceef392015-12-29 15:58:39 -05001754 set_delayed_call(done, kfree_link, buf);
Al Viro680baac2015-05-02 13:32:22 -04001755 return buf;
Al Viroc177c2a2010-01-14 00:59:16 -05001756}
1757
David Teiglandb3b94fa2006-01-16 16:50:04 +00001758/**
1759 * gfs2_permission -
Steven Whitehouse75d5cfb2011-01-19 09:42:40 +00001760 * @inode: The inode
1761 * @mask: The mask to be tested
1762 * @flags: Indicates whether this is an RCU path walk or not
David Teiglandb3b94fa2006-01-16 16:50:04 +00001763 *
Steven Whitehouse300c7d72006-11-27 09:55:28 -05001764 * 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 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001768 * Returns: errno
1769 */
1770
Al Viro10556cb2011-06-20 19:28:19 -04001771int gfs2_permission(struct inode *inode, int mask)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001772{
Nick Pigginb74c79e2011-01-07 17:49:58 +11001773 struct gfs2_inode *ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001774 struct gfs2_holder i_gh;
1775 int error;
Steven Whitehouse300c7d72006-11-27 09:55:28 -05001776 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001777
Nick Pigginb74c79e2011-01-07 17:49:58 +11001778
1779 ip = GFS2_I(inode);
Steven Whitehouse7afd88d2008-02-22 16:07:18 +00001780 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Benjamin Marzinski2e60d762014-11-13 20:42:04 -06001781 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;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001787 }
1788
Miklos Szeredif58ba882008-07-02 21:12:01 +02001789 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
1790 error = -EACCES;
1791 else
Al Viro2830ba72011-06-20 19:16:29 -04001792 error = generic_permission(inode, mask);
Steven Whitehouse300c7d72006-11-27 09:55:28 -05001793 if (unlock)
1794 gfs2_glock_dq_uninit(&i_gh);
1795
David Teiglandb3b94fa2006-01-16 16:50:04 +00001796 return error;
1797}
1798
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001799static int __gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
Steven Whitehouse194c0112011-05-09 14:06:38 +01001800{
Steven Whitehouse194c0112011-05-09 14:06:38 +01001801 setattr_copy(inode, attr);
1802 mark_inode_dirty(inode);
Steven Whitehouse194c0112011-05-09 14:06:38 +01001803 return 0;
1804}
1805
1806/**
1807 * gfs2_setattr_simple -
1808 * @ip:
1809 * @attr:
1810 *
1811 * Returns: errno
1812 */
1813
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001814int gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
Steven Whitehouse194c0112011-05-09 14:06:38 +01001815{
1816 int error;
1817
1818 if (current->journal_info)
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001819 return __gfs2_setattr_simple(inode, attr);
Steven Whitehouse194c0112011-05-09 14:06:38 +01001820
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001821 error = gfs2_trans_begin(GFS2_SB(inode), RES_DINODE, 0);
Steven Whitehouse194c0112011-05-09 14:06:38 +01001822 if (error)
1823 return error;
1824
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001825 error = __gfs2_setattr_simple(inode, attr);
1826 gfs2_trans_end(GFS2_SB(inode));
Steven Whitehouse194c0112011-05-09 14:06:38 +01001827 return error;
1828}
1829
David Teiglandb3b94fa2006-01-16 16:50:04 +00001830static int setattr_chown(struct inode *inode, struct iattr *attr)
1831{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001832 struct gfs2_inode *ip = GFS2_I(inode);
1833 struct gfs2_sbd *sdp = GFS2_SB(inode);
Eric W. Biederman7c06b5d2013-01-31 20:27:54 -08001834 kuid_t ouid, nuid;
1835 kgid_t ogid, ngid;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001836 int error;
Abhi Dasb8fbf472015-03-18 12:03:41 -05001837 struct gfs2_alloc_parms ap;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001838
Steven Whitehouse2933f922006-11-01 13:23:29 -05001839 ouid = inode->i_uid;
1840 ogid = inode->i_gid;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001841 nuid = attr->ia_uid;
1842 ngid = attr->ia_gid;
1843
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -08001844 if (!(attr->ia_valid & ATTR_UID) || uid_eq(ouid, nuid))
Eric W. Biedermanf4108a62013-01-31 17:49:26 -08001845 ouid = nuid = NO_UID_QUOTA_CHANGE;
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -08001846 if (!(attr->ia_valid & ATTR_GID) || gid_eq(ogid, ngid))
Eric W. Biedermanf4108a62013-01-31 17:49:26 -08001847 ogid = ngid = NO_GID_QUOTA_CHANGE;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001848
Bob Petersonb54e9a02015-10-26 10:40:28 -05001849 error = gfs2_rsqa_alloc(ip);
Bob Peterson62e96cf2014-01-06 17:16:01 -05001850 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
Abhi Dasb8fbf472015-03-18 12:03:41 -05001861 ap.target = gfs2_get_inode_blocks(&ip->i_inode);
1862
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -08001863 if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
1864 !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
Abhi Dasb8fbf472015-03-18 12:03:41 -05001865 error = gfs2_quota_check(ip, nuid, ngid, &ap);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001866 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
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001874 error = gfs2_setattr_simple(inode, attr);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001875 if (error)
1876 goto out_end_trans;
1877
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -08001878 if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
1879 !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
Abhi Das39a72582015-06-02 11:02:24 -05001880 gfs2_quota_change(ip, -(s64)ap.target, ouid, ogid);
Abhi Dasb8fbf472015-03-18 12:03:41 -05001881 gfs2_quota_change(ip, ap.target, nuid, ngid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001882 }
1883
Steven Whitehousea91ea692006-09-04 12:04:26 -04001884out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001885 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001886out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001887 gfs2_quota_unlock(ip);
Bob Peterson62e96cf2014-01-06 17:16:01 -05001888out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001889 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{
David Howells2b0143b2015-03-17 22:25:59 +00001905 struct inode *inode = d_inode(dentry);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001906 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001907 struct gfs2_holder i_gh;
1908 int error;
1909
Bob Petersonb54e9a02015-10-26 10:40:28 -05001910 error = gfs2_rsqa_alloc(ip);
Bob Peterson0a305e42012-06-06 11:17:59 +01001911 if (error)
1912 return error;
1913
David Teiglandb3b94fa2006-01-16 16:50:04 +00001914 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)
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001927 error = gfs2_setattr_size(inode, attr->ia_size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001928 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1929 error = setattr_chown(inode, attr);
Christoph Hellwige01580b2013-12-20 05:16:52 -08001930 else {
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001931 error = gfs2_setattr_simple(inode, attr);
Christoph Hellwige01580b2013-12-20 05:16:52 -08001932 if (!error && attr->ia_valid & ATTR_MODE)
1933 error = posix_acl_chmod(inode, inode->i_mode);
1934 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001935
Steven Whitehousea91ea692006-09-04 12:04:26 -04001936out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001937 if (!error)
1938 mark_inode_dirty(inode);
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001939 gfs2_glock_dq_uninit(&i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001940 return error;
1941}
1942
1943/**
1944 * gfs2_getattr - Read out an inode's attributes
Steven Whitehouse26c1a572006-09-04 15:32:10 -04001945 * @mnt: The vfsmount the inode is being accessed from
David Teiglandb3b94fa2006-01-16 16:50:04 +00001946 * @dentry: The dentry to stat
1947 * @stat: The inode's stats
1948 *
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001949 * 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 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001955 * Returns: errno
1956 */
1957
1958static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1959 struct kstat *stat)
1960{
David Howells2b0143b2015-03-17 22:25:59 +00001961 struct inode *inode = d_inode(dentry);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001962 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001963 struct gfs2_holder gh;
1964 int error;
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001965 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001966
Steven Whitehouse7afd88d2008-02-22 16:07:18 +00001967 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Benjamin Marzinski2e60d762014-11-13 20:42:04 -06001968 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1969 if (error)
1970 return error;
1971 unlock = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001972 }
1973
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001974 generic_fillattr(inode, stat);
Steven Whitehoused7c103d2007-01-25 17:14:59 +00001975 if (unlock)
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001976 gfs2_glock_dq_uninit(&gh);
1977
1978 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001979}
1980
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001981static 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
Al Viro59551022016-01-22 15:40:57 -05001992 inode_lock(inode);
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001993
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:
Al Viro59551022016-01-22 15:40:57 -05002019 inode_unlock(inode);
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002020 return ret;
2021}
2022
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002023const struct inode_operations gfs2_file_iops = {
Al Viroe6305c42008-07-15 21:03:57 -04002024 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002025 .setattr = gfs2_setattr,
2026 .getattr = gfs2_getattr,
Al Viro1a39ba92016-05-13 03:59:17 +02002027 .setxattr = generic_setxattr,
2028 .getxattr = generic_getxattr,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002029 .listxattr = gfs2_listxattr,
Al Viro1a39ba92016-05-13 03:59:17 +02002030 .removexattr = generic_removexattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002031 .fiemap = gfs2_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02002032 .get_acl = gfs2_get_acl,
Christoph Hellwige01580b2013-12-20 05:16:52 -08002033 .set_acl = gfs2_set_acl,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002034};
2035
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002036const struct inode_operations gfs2_dir_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00002037 .create = gfs2_create,
2038 .lookup = gfs2_lookup,
2039 .link = gfs2_link,
2040 .unlink = gfs2_unlink,
2041 .symlink = gfs2_symlink,
2042 .mkdir = gfs2_mkdir,
Steven Whitehouse855d23c2011-05-09 16:42:37 +01002043 .rmdir = gfs2_unlink,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002044 .mknod = gfs2_mknod,
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05002045 .rename2 = gfs2_rename2,
Al Viroe6305c42008-07-15 21:03:57 -04002046 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002047 .setattr = gfs2_setattr,
2048 .getattr = gfs2_getattr,
Al Viro1a39ba92016-05-13 03:59:17 +02002049 .setxattr = generic_setxattr,
2050 .getxattr = generic_getxattr,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002051 .listxattr = gfs2_listxattr,
Al Viro1a39ba92016-05-13 03:59:17 +02002052 .removexattr = generic_removexattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002053 .fiemap = gfs2_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02002054 .get_acl = gfs2_get_acl,
Christoph Hellwige01580b2013-12-20 05:16:52 -08002055 .set_acl = gfs2_set_acl,
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01002056 .atomic_open = gfs2_atomic_open,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002057};
2058
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002059const struct inode_operations gfs2_symlink_iops = {
Al Viroc177c2a2010-01-14 00:59:16 -05002060 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -05002061 .get_link = gfs2_get_link,
Al Viroe6305c42008-07-15 21:03:57 -04002062 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002063 .setattr = gfs2_setattr,
2064 .getattr = gfs2_getattr,
Al Viro1a39ba92016-05-13 03:59:17 +02002065 .setxattr = generic_setxattr,
2066 .getxattr = generic_getxattr,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002067 .listxattr = gfs2_listxattr,
Al Viro1a39ba92016-05-13 03:59:17 +02002068 .removexattr = generic_removexattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002069 .fiemap = gfs2_fiemap,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002070};
2071