blob: 12cbea7502c26040fb90db5750e764bdd831079a [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehouse3a8a9a12006-05-18 15:09:15 -04003 * Copyright (C) 2004-2006 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>
Benjamin Marzinski39211202010-08-20 00:21:02 -050021#include <linux/swap.h>
22#include <linux/falloc.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000023#include <asm/uaccess.h>
24
25#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050026#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000027#include "acl.h"
28#include "bmap.h"
29#include "dir.h"
Steven Whitehouse307cf6e2009-08-26 18:51:04 +010030#include "xattr.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000031#include "glock.h"
32#include "inode.h"
33#include "meta_io.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000034#include "quota.h"
35#include "rgrp.h"
36#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050037#include "util.h"
Steven Whitehouseb2760582008-10-14 16:05:55 +010038#include "super.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000039
40/**
41 * gfs2_create - Create a file
42 * @dir: The directory in which to create the file
43 * @dentry: The dentry of the new file
44 * @mode: The mode of the new file
45 *
46 * Returns: errno
47 */
48
49static int gfs2_create(struct inode *dir, struct dentry *dentry,
50 int mode, struct nameidata *nd)
51{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040052 struct gfs2_inode *dip = GFS2_I(dir);
53 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +000054 struct gfs2_holder ghs[2];
55 struct inode *inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +000056
David Teiglandb3b94fa2006-01-16 16:50:04 +000057 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
58
59 for (;;) {
Steven Whitehousee7f14f42006-10-31 21:45:08 -050060 inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +000061 if (!IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +000062 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +000063 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +000064 gfs2_inplace_release(dip);
65 gfs2_quota_unlock(dip);
66 gfs2_alloc_put(dip);
67 gfs2_glock_dq_uninit_m(2, ghs);
Steven Whitehouse3a8476d2006-06-19 09:10:39 -040068 mark_inode_dirty(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000069 break;
Steven Whitehouse7359a192006-02-13 12:27:43 +000070 } else if (PTR_ERR(inode) != -EEXIST ||
Al Viro35165862008-08-05 03:00:49 -040071 (nd && nd->flags & LOOKUP_EXCL)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +000072 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +000073 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000074 }
75
Al Viroa569c712008-07-23 14:42:05 -040076 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
Steven Whitehousec7526662006-03-20 12:30:04 -050077 if (inode) {
78 if (!IS_ERR(inode)) {
Steven Whitehousec7526662006-03-20 12:30:04 -050079 gfs2_holder_uninit(ghs);
80 break;
81 } else {
82 gfs2_holder_uninit(ghs);
83 return PTR_ERR(inode);
84 }
David Teiglandb3b94fa2006-01-16 16:50:04 +000085 }
86 }
87
David Teiglandb3b94fa2006-01-16 16:50:04 +000088 d_instantiate(dentry, inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000089
90 return 0;
91}
92
93/**
94 * gfs2_lookup - Look up a filename in a directory and return its inode
95 * @dir: The directory inode
96 * @dentry: The dentry of the new inode
97 * @nd: passed from Linux VFS, ignored by us
98 *
99 * Called by the VFS layer. Lock dir and call gfs2_lookupi()
100 *
101 * Returns: errno
102 */
103
104static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
105 struct nameidata *nd)
106{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000107 struct inode *inode = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000108
Steven Whitehousec7526662006-03-20 12:30:04 -0500109 dentry->d_op = &gfs2_dops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000110
Al Viroa569c712008-07-23 14:42:05 -0400111 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
Steven Whitehousec7526662006-03-20 12:30:04 -0500112 if (inode && IS_ERR(inode))
David Howellse231c2e2008-02-07 00:15:26 -0800113 return ERR_CAST(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000114
Steven Whitehouse9656b2c2008-01-08 08:14:30 +0000115 if (inode) {
116 struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
117 struct gfs2_holder gh;
118 int error;
119 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
120 if (error) {
121 iput(inode);
122 return ERR_PTR(error);
123 }
124 gfs2_glock_dq_uninit(&gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000125 return d_splice_alias(inode, dentry);
Steven Whitehouse9656b2c2008-01-08 08:14:30 +0000126 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000127 d_add(dentry, inode);
128
129 return NULL;
130}
131
132/**
133 * gfs2_link - Link to a file
134 * @old_dentry: The inode to link
135 * @dir: Add link to this directory
136 * @dentry: The name of the link
137 *
138 * Link the inode in "old_dentry" into the directory "dir" with the
139 * name in "dentry".
140 *
141 * Returns: errno
142 */
143
144static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
145 struct dentry *dentry)
146{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400147 struct gfs2_inode *dip = GFS2_I(dir);
148 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000149 struct inode *inode = old_dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400150 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000151 struct gfs2_holder ghs[2];
152 int alloc_required;
153 int error;
154
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500155 if (S_ISDIR(inode->i_mode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000156 return -EPERM;
157
158 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
159 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
160
Bob Peterson72dbf472008-08-12 13:39:29 -0500161 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000162 if (error)
Bob Peterson72dbf472008-08-12 13:39:29 -0500163 goto out_parent;
164
165 error = gfs2_glock_nq(ghs + 1); /* child */
166 if (error)
167 goto out_child;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000168
Miklos Szeredif58ba882008-07-02 21:12:01 +0200169 error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000170 if (error)
171 goto out_gunlock;
172
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100173 error = gfs2_dir_check(dir, &dentry->d_name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000174 switch (error) {
175 case -ENOENT:
176 break;
177 case 0:
178 error = -EEXIST;
179 default:
180 goto out_gunlock;
181 }
182
183 error = -EINVAL;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500184 if (!dip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000185 goto out_gunlock;
186 error = -EFBIG;
Steven Whitehousead6203f2008-11-03 13:59:19 +0000187 if (dip->i_entries == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000188 goto out_gunlock;
189 error = -EPERM;
190 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
191 goto out_gunlock;
192 error = -EINVAL;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500193 if (!ip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000194 goto out_gunlock;
195 error = -EMLINK;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500196 if (ip->i_inode.i_nlink == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000197 goto out_gunlock;
198
Steven Whitehousec7526662006-03-20 12:30:04 -0500199 alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
200 if (error < 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000201 goto out_gunlock;
Steven Whitehousec7526662006-03-20 12:30:04 -0500202 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000203
204 if (alloc_required) {
205 struct gfs2_alloc *al = gfs2_alloc_get(dip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300206 if (!al) {
207 error = -ENOMEM;
208 goto out_gunlock;
209 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000210
Steven Whitehoused82661d2008-03-10 15:34:50 +0000211 error = gfs2_quota_lock_check(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000212 if (error)
213 goto out_alloc;
214
David Teiglandb3b94fa2006-01-16 16:50:04 +0000215 al->al_requested = sdp->sd_max_dirres;
216
217 error = gfs2_inplace_reserve(dip);
218 if (error)
219 goto out_gunlock_q;
220
Steven Whitehouse1b502592006-05-18 14:10:52 -0400221 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
Benjamin Marzinskibf97b672010-09-27 16:00:04 -0500222 gfs2_rg_blocks(al) +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000223 2 * RES_DINODE + RES_STATFS +
224 RES_QUOTA, 0);
225 if (error)
226 goto out_ipres;
227 } else {
228 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
229 if (error)
230 goto out_ipres;
231 }
232
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100233 error = gfs2_dir_add(dir, &dentry->d_name, ip, IF2DT(inode->i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000234 if (error)
235 goto out_end_trans;
236
237 error = gfs2_change_nlink(ip, +1);
238
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400239out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000240 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400241out_ipres:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000242 if (alloc_required)
243 gfs2_inplace_release(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400244out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000245 if (alloc_required)
246 gfs2_quota_unlock(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400247out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000248 if (alloc_required)
249 gfs2_alloc_put(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400250out_gunlock:
Bob Peterson72dbf472008-08-12 13:39:29 -0500251 gfs2_glock_dq(ghs + 1);
252out_child:
253 gfs2_glock_dq(ghs);
254out_parent:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000255 gfs2_holder_uninit(ghs);
256 gfs2_holder_uninit(ghs + 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000257 if (!error) {
Al Viro7de9c6e2010-10-23 11:11:40 -0400258 ihold(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000259 d_instantiate(dentry, inode);
260 mark_inode_dirty(inode);
261 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000262 return error;
263}
264
Steven Whitehouse87ec2172009-05-22 10:54:50 +0100265/*
266 * gfs2_unlink_ok - check to see that a inode is still in a directory
267 * @dip: the directory
268 * @name: the name of the file
269 * @ip: the inode
270 *
271 * Assumes that the lock on (at least) @dip is held.
272 *
273 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
274 */
275
276static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
277 const struct gfs2_inode *ip)
278{
279 int error;
280
281 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
282 return -EPERM;
283
284 if ((dip->i_inode.i_mode & S_ISVTX) &&
285 dip->i_inode.i_uid != current_fsuid() &&
286 ip->i_inode.i_uid != current_fsuid() && !capable(CAP_FOWNER))
287 return -EPERM;
288
289 if (IS_APPEND(&dip->i_inode))
290 return -EPERM;
291
292 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
293 if (error)
294 return error;
295
296 error = gfs2_dir_check(&dip->i_inode, name, ip);
297 if (error)
298 return error;
299
300 return 0;
301}
302
David Teiglandb3b94fa2006-01-16 16:50:04 +0000303/**
304 * gfs2_unlink - Unlink a file
305 * @dir: The inode of the directory containing the file to unlink
306 * @dentry: The file itself
307 *
308 * Unlink a file. Call gfs2_unlinki()
309 *
310 * Returns: errno
311 */
312
313static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
314{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400315 struct gfs2_inode *dip = GFS2_I(dir);
316 struct gfs2_sbd *sdp = GFS2_SB(dir);
317 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
Russell Cattelanddee7602007-01-29 17:13:44 -0600318 struct gfs2_holder ghs[3];
319 struct gfs2_rgrpd *rgd;
320 struct gfs2_holder ri_gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000321 int error;
322
Russell Cattelanddee7602007-01-29 17:13:44 -0600323 error = gfs2_rindex_hold(sdp, &ri_gh);
324 if (error)
325 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000326
Russell Cattelanddee7602007-01-29 17:13:44 -0600327 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
328 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
329
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100330 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
Russell Cattelanddee7602007-01-29 17:13:44 -0600331 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
332
333
Steven Whitehouse8497a462007-08-26 14:23:56 +0100334 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000335 if (error)
Steven Whitehouse8497a462007-08-26 14:23:56 +0100336 goto out_parent;
337
338 error = gfs2_glock_nq(ghs + 1); /* child */
339 if (error)
340 goto out_child;
341
342 error = gfs2_glock_nq(ghs + 2); /* rgrp */
343 if (error)
344 goto out_rgrp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000345
346 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
347 if (error)
Bob Peterson72dbf472008-08-12 13:39:29 -0500348 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000349
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400350 error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000351 if (error)
Roel Kluincd012072009-08-22 19:26:42 +0200352 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000353
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400354 error = gfs2_dir_del(dip, &dentry->d_name);
355 if (error)
356 goto out_end_trans;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000357
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400358 error = gfs2_change_nlink(ip, -1);
359
360out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000361 gfs2_trans_end(sdp);
Bob Peterson72dbf472008-08-12 13:39:29 -0500362out_gunlock:
Steven Whitehouse8497a462007-08-26 14:23:56 +0100363 gfs2_glock_dq(ghs + 2);
364out_rgrp:
Russell Cattelanddee7602007-01-29 17:13:44 -0600365 gfs2_holder_uninit(ghs + 2);
Steven Whitehouse8497a462007-08-26 14:23:56 +0100366 gfs2_glock_dq(ghs + 1);
367out_child:
368 gfs2_holder_uninit(ghs + 1);
369 gfs2_glock_dq(ghs);
370out_parent:
371 gfs2_holder_uninit(ghs);
Russell Cattelanddee7602007-01-29 17:13:44 -0600372 gfs2_glock_dq_uninit(&ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000373 return error;
374}
375
376/**
377 * gfs2_symlink - Create a symlink
378 * @dir: The directory to create the symlink in
379 * @dentry: The dentry to put the symlink in
380 * @symname: The thing which the link points to
381 *
382 * Returns: errno
383 */
384
385static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
386 const char *symname)
387{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400388 struct gfs2_inode *dip = GFS2_I(dir), *ip;
389 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000390 struct gfs2_holder ghs[2];
391 struct inode *inode;
392 struct buffer_head *dibh;
393 int size;
394 int error;
395
David Teiglandb3b94fa2006-01-16 16:50:04 +0000396 /* Must be stuffed with a null terminator for gfs2_follow_link() */
397 size = strlen(symname);
398 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
399 return -ENAMETOOLONG;
400
401 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
402
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500403 inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000404 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000405 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000406 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000407 }
408
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500409 ip = ghs[1].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000410
Steven Whitehouse5cf32522009-03-31 16:06:27 +0100411 i_size_write(inode, size);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000412
413 error = gfs2_meta_inode_buffer(ip, &dibh);
414
415 if (!gfs2_assert_withdraw(sdp, !error)) {
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500416 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000417 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
418 size);
419 brelse(dibh);
420 }
421
422 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000423 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000424 gfs2_inplace_release(dip);
425 gfs2_quota_unlock(dip);
426 gfs2_alloc_put(dip);
427
428 gfs2_glock_dq_uninit_m(2, ghs);
429
David Teiglandb3b94fa2006-01-16 16:50:04 +0000430 d_instantiate(dentry, inode);
431 mark_inode_dirty(inode);
432
433 return 0;
434}
435
436/**
437 * gfs2_mkdir - Make a directory
438 * @dir: The parent directory of the new one
439 * @dentry: The dentry of the new directory
440 * @mode: The mode of the new directory
441 *
442 * Returns: errno
443 */
444
445static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
446{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400447 struct gfs2_inode *dip = GFS2_I(dir), *ip;
448 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000449 struct gfs2_holder ghs[2];
450 struct inode *inode;
451 struct buffer_head *dibh;
452 int error;
453
David Teiglandb3b94fa2006-01-16 16:50:04 +0000454 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
455
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500456 inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000457 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000458 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000459 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000460 }
461
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500462 ip = ghs[1].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000463
Steven Whitehouse4f561102006-11-01 14:04:17 -0500464 ip->i_inode.i_nlink = 2;
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100465 i_size_write(inode, sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode));
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000466 ip->i_diskflags |= GFS2_DIF_JDATA;
Steven Whitehousead6203f2008-11-03 13:59:19 +0000467 ip->i_entries = 2;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000468
469 error = gfs2_meta_inode_buffer(ip, &dibh);
470
471 if (!gfs2_assert_withdraw(sdp, !error)) {
472 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
Steven Whitehousec7526662006-03-20 12:30:04 -0500473 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000474
Steven Whitehousec7526662006-03-20 12:30:04 -0500475 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse8d123582010-09-17 12:30:23 +0100476 gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000477 dent->de_inum = di->di_num; /* already GFS2 endian */
Steven Whitehouse7ecdb702006-10-03 21:03:35 -0400478 dent->de_type = cpu_to_be16(DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000479 di->di_entries = cpu_to_be32(1);
480
Steven Whitehousec7526662006-03-20 12:30:04 -0500481 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
Steven Whitehouse8d123582010-09-17 12:30:23 +0100482 gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000483
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100484 gfs2_inum_out(dip, dent);
Steven Whitehouse7ecdb702006-10-03 21:03:35 -0400485 dent->de_type = cpu_to_be16(DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000486
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500487 gfs2_dinode_out(ip, di);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000488
489 brelse(dibh);
490 }
491
492 error = gfs2_change_nlink(dip, +1);
493 gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
494
495 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000496 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000497 gfs2_inplace_release(dip);
498 gfs2_quota_unlock(dip);
499 gfs2_alloc_put(dip);
500
501 gfs2_glock_dq_uninit_m(2, ghs);
502
David Teiglandb3b94fa2006-01-16 16:50:04 +0000503 d_instantiate(dentry, inode);
504 mark_inode_dirty(inode);
505
506 return 0;
507}
508
509/**
Steven Whitehouse2286dbf2009-05-22 10:45:09 +0100510 * gfs2_rmdiri - Remove a directory
511 * @dip: The parent directory of the directory to be removed
512 * @name: The name of the directory to be removed
513 * @ip: The GFS2 inode of the directory to be removed
514 *
515 * Assumes Glocks on dip and ip are held
516 *
517 * Returns: errno
518 */
519
520static int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
521 struct gfs2_inode *ip)
522{
Steven Whitehouse2286dbf2009-05-22 10:45:09 +0100523 int error;
524
525 if (ip->i_entries != 2) {
526 if (gfs2_consist_inode(ip))
527 gfs2_dinode_print(ip);
528 return -EIO;
529 }
530
531 error = gfs2_dir_del(dip, name);
532 if (error)
533 return error;
534
535 error = gfs2_change_nlink(dip, -1);
536 if (error)
537 return error;
538
Steven Whitehouse8d123582010-09-17 12:30:23 +0100539 error = gfs2_dir_del(ip, &gfs2_qdot);
Steven Whitehouse2286dbf2009-05-22 10:45:09 +0100540 if (error)
541 return error;
542
Steven Whitehouse8d123582010-09-17 12:30:23 +0100543 error = gfs2_dir_del(ip, &gfs2_qdotdot);
Steven Whitehouse2286dbf2009-05-22 10:45:09 +0100544 if (error)
545 return error;
546
547 /* It looks odd, but it really should be done twice */
548 error = gfs2_change_nlink(ip, -1);
549 if (error)
550 return error;
551
552 error = gfs2_change_nlink(ip, -1);
553 if (error)
554 return error;
555
556 return error;
557}
558
559/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000560 * gfs2_rmdir - Remove a directory
561 * @dir: The parent directory of the directory to be removed
562 * @dentry: The dentry of the directory to remove
563 *
564 * Remove a directory. Call gfs2_rmdiri()
565 *
566 * Returns: errno
567 */
568
569static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
570{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400571 struct gfs2_inode *dip = GFS2_I(dir);
572 struct gfs2_sbd *sdp = GFS2_SB(dir);
573 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
Russell Cattelanddee7602007-01-29 17:13:44 -0600574 struct gfs2_holder ghs[3];
575 struct gfs2_rgrpd *rgd;
576 struct gfs2_holder ri_gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000577 int error;
578
Russell Cattelanddee7602007-01-29 17:13:44 -0600579 error = gfs2_rindex_hold(sdp, &ri_gh);
580 if (error)
581 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000582 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
583 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
584
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100585 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
Russell Cattelanddee7602007-01-29 17:13:44 -0600586 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
587
Bob Peterson72dbf472008-08-12 13:39:29 -0500588 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000589 if (error)
Bob Peterson72dbf472008-08-12 13:39:29 -0500590 goto out_parent;
591
592 error = gfs2_glock_nq(ghs + 1); /* child */
593 if (error)
594 goto out_child;
595
596 error = gfs2_glock_nq(ghs + 2); /* rgrp */
597 if (error)
598 goto out_rgrp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000599
600 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
601 if (error)
602 goto out_gunlock;
603
Steven Whitehousead6203f2008-11-03 13:59:19 +0000604 if (ip->i_entries < 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000605 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500606 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000607 error = -EIO;
608 goto out_gunlock;
609 }
Steven Whitehousead6203f2008-11-03 13:59:19 +0000610 if (ip->i_entries > 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000611 error = -ENOTEMPTY;
612 goto out_gunlock;
613 }
614
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400615 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000616 if (error)
617 goto out_gunlock;
618
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400619 error = gfs2_rmdiri(dip, &dentry->d_name, ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000620
621 gfs2_trans_end(sdp);
622
Steven Whitehousea91ea692006-09-04 12:04:26 -0400623out_gunlock:
Bob Peterson72dbf472008-08-12 13:39:29 -0500624 gfs2_glock_dq(ghs + 2);
625out_rgrp:
Russell Cattelanddee7602007-01-29 17:13:44 -0600626 gfs2_holder_uninit(ghs + 2);
Bob Peterson72dbf472008-08-12 13:39:29 -0500627 gfs2_glock_dq(ghs + 1);
628out_child:
629 gfs2_holder_uninit(ghs + 1);
630 gfs2_glock_dq(ghs);
631out_parent:
632 gfs2_holder_uninit(ghs);
Russell Cattelanddee7602007-01-29 17:13:44 -0600633 gfs2_glock_dq_uninit(&ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000634 return error;
635}
636
637/**
638 * gfs2_mknod - Make a special file
639 * @dir: The directory in which the special file will reside
640 * @dentry: The dentry of the special file
641 * @mode: The mode of the special file
642 * @rdev: The device specification of the special file
643 *
644 */
645
646static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
647 dev_t dev)
648{
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500649 struct gfs2_inode *dip = GFS2_I(dir);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400650 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000651 struct gfs2_holder ghs[2];
652 struct inode *inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000653
654 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
655
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500656 inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000657 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000658 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000659 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000660 }
661
David Teiglandb3b94fa2006-01-16 16:50:04 +0000662 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000663 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000664 gfs2_inplace_release(dip);
665 gfs2_quota_unlock(dip);
666 gfs2_alloc_put(dip);
667
668 gfs2_glock_dq_uninit_m(2, ghs);
669
David Teiglandb3b94fa2006-01-16 16:50:04 +0000670 d_instantiate(dentry, inode);
671 mark_inode_dirty(inode);
672
673 return 0;
674}
675
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100676/*
677 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
678 * @this: move this
679 * @to: to here
680 *
681 * Follow @to back to the root and make sure we don't encounter @this
682 * Assumes we already hold the rename lock.
683 *
684 * Returns: errno
685 */
686
687static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
688{
689 struct inode *dir = &to->i_inode;
690 struct super_block *sb = dir->i_sb;
691 struct inode *tmp;
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100692 int error = 0;
693
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100694 igrab(dir);
695
696 for (;;) {
697 if (dir == &this->i_inode) {
698 error = -EINVAL;
699 break;
700 }
701 if (dir == sb->s_root->d_inode) {
702 error = 0;
703 break;
704 }
705
Steven Whitehouse8d123582010-09-17 12:30:23 +0100706 tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100707 if (IS_ERR(tmp)) {
708 error = PTR_ERR(tmp);
709 break;
710 }
711
712 iput(dir);
713 dir = tmp;
714 }
715
716 iput(dir);
717
718 return error;
719}
720
David Teiglandb3b94fa2006-01-16 16:50:04 +0000721/**
722 * gfs2_rename - Rename a file
723 * @odir: Parent directory of old file name
724 * @odentry: The old dentry of the file
725 * @ndir: Parent directory of new file name
726 * @ndentry: The new dentry of the file
727 *
728 * Returns: errno
729 */
730
731static int gfs2_rename(struct inode *odir, struct dentry *odentry,
732 struct inode *ndir, struct dentry *ndentry)
733{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400734 struct gfs2_inode *odip = GFS2_I(odir);
735 struct gfs2_inode *ndip = GFS2_I(ndir);
736 struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000737 struct gfs2_inode *nip = NULL;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400738 struct gfs2_sbd *sdp = GFS2_SB(odir);
Bob Peterson46290342010-09-30 10:34:00 -0400739 struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, }, ri_gh;
Russell Cattelanddee7602007-01-29 17:13:44 -0600740 struct gfs2_rgrpd *nrgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000741 unsigned int num_gh;
742 int dir_rename = 0;
Steven Whitehouse24b977b2009-12-09 13:55:12 +0000743 int alloc_required = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000744 unsigned int x;
745 int error;
746
David Teiglandb3b94fa2006-01-16 16:50:04 +0000747 if (ndentry->d_inode) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400748 nip = GFS2_I(ndentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000749 if (ip == nip)
750 return 0;
751 }
752
Bob Peterson46290342010-09-30 10:34:00 -0400753 error = gfs2_rindex_hold(sdp, &ri_gh);
754 if (error)
755 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000756
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100757 if (odip != ndip) {
758 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
759 0, &r_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000760 if (error)
761 goto out;
762
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100763 if (S_ISDIR(ip->i_inode.i_mode)) {
764 dir_rename = 1;
765 /* don't move a dirctory into it's subdir */
766 error = gfs2_ok_to_move(ip, ndip);
767 if (error)
768 goto out_gunlock_r;
769 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000770 }
771
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400772 num_gh = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000773 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400774 if (odip != ndip) {
775 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
776 num_gh++;
777 }
778 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
779 num_gh++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000780
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400781 if (nip) {
782 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
783 num_gh++;
Russell Cattelanddee7602007-01-29 17:13:44 -0600784 /* grab the resource lock for unlink flag twiddling
785 * this is the case of the target file already existing
786 * so we unlink before doing the rename
787 */
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100788 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
Russell Cattelanddee7602007-01-29 17:13:44 -0600789 if (nrgd)
790 gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400791 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000792
Bob Peterson72dbf472008-08-12 13:39:29 -0500793 for (x = 0; x < num_gh; x++) {
794 error = gfs2_glock_nq(ghs + x);
795 if (error)
796 goto out_gunlock;
797 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000798
799 /* Check out the old directory */
800
801 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
802 if (error)
803 goto out_gunlock;
804
805 /* Check out the new directory */
806
807 if (nip) {
808 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
809 if (error)
810 goto out_gunlock;
811
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500812 if (S_ISDIR(nip->i_inode.i_mode)) {
Steven Whitehousead6203f2008-11-03 13:59:19 +0000813 if (nip->i_entries < 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000814 if (gfs2_consist_inode(nip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500815 gfs2_dinode_print(nip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000816 error = -EIO;
817 goto out_gunlock;
818 }
Steven Whitehousead6203f2008-11-03 13:59:19 +0000819 if (nip->i_entries > 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000820 error = -ENOTEMPTY;
821 goto out_gunlock;
822 }
823 }
824 } else {
Miklos Szeredif58ba882008-07-02 21:12:01 +0200825 error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000826 if (error)
827 goto out_gunlock;
828
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100829 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000830 switch (error) {
831 case -ENOENT:
832 error = 0;
833 break;
834 case 0:
835 error = -EEXIST;
836 default:
837 goto out_gunlock;
838 };
839
840 if (odip != ndip) {
Steven Whitehouse4f561102006-11-01 14:04:17 -0500841 if (!ndip->i_inode.i_nlink) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000842 error = -EINVAL;
843 goto out_gunlock;
844 }
Steven Whitehousead6203f2008-11-03 13:59:19 +0000845 if (ndip->i_entries == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000846 error = -EFBIG;
847 goto out_gunlock;
848 }
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500849 if (S_ISDIR(ip->i_inode.i_mode) &&
Steven Whitehouse4f561102006-11-01 14:04:17 -0500850 ndip->i_inode.i_nlink == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000851 error = -EMLINK;
852 goto out_gunlock;
853 }
854 }
855 }
856
857 /* Check out the dir to be renamed */
858
859 if (dir_rename) {
Miklos Szeredif58ba882008-07-02 21:12:01 +0200860 error = gfs2_permission(odentry->d_inode, MAY_WRITE);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000861 if (error)
862 goto out_gunlock;
863 }
864
Steven Whitehouse24b977b2009-12-09 13:55:12 +0000865 if (nip == NULL)
866 alloc_required = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
867 error = alloc_required;
Steven Whitehousec7526662006-03-20 12:30:04 -0500868 if (error < 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000869 goto out_gunlock;
Steven Whitehousec7526662006-03-20 12:30:04 -0500870 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000871
872 if (alloc_required) {
873 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300874 if (!al) {
875 error = -ENOMEM;
876 goto out_gunlock;
877 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000878
Steven Whitehoused82661d2008-03-10 15:34:50 +0000879 error = gfs2_quota_lock_check(ndip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000880 if (error)
881 goto out_alloc;
882
David Teiglandb3b94fa2006-01-16 16:50:04 +0000883 al->al_requested = sdp->sd_max_dirres;
884
Bob Peterson46290342010-09-30 10:34:00 -0400885 error = gfs2_inplace_reserve_ri(ndip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000886 if (error)
887 goto out_gunlock_q;
888
Steven Whitehousefe1bded2006-04-18 10:09:15 -0400889 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
Benjamin Marzinskibf97b672010-09-27 16:00:04 -0500890 gfs2_rg_blocks(al) +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000891 4 * RES_DINODE + 4 * RES_LEAF +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500892 RES_STATFS + RES_QUOTA + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000893 if (error)
894 goto out_ipreserv;
895 } else {
896 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500897 5 * RES_LEAF + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000898 if (error)
899 goto out_gunlock;
900 }
901
902 /* Remove the target file, if it exists */
903
904 if (nip) {
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500905 if (S_ISDIR(nip->i_inode.i_mode))
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400906 error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
907 else {
908 error = gfs2_dir_del(ndip, &ndentry->d_name);
909 if (error)
910 goto out_end_trans;
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500911 error = gfs2_change_nlink(nip, -1);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400912 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000913 if (error)
914 goto out_end_trans;
915 }
916
917 if (dir_rename) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000918 error = gfs2_change_nlink(ndip, +1);
919 if (error)
920 goto out_end_trans;
921 error = gfs2_change_nlink(odip, -1);
922 if (error)
923 goto out_end_trans;
924
Steven Whitehouse8d123582010-09-17 12:30:23 +0100925 error = gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000926 if (error)
927 goto out_end_trans;
928 } else {
929 struct buffer_head *dibh;
930 error = gfs2_meta_inode_buffer(ip, &dibh);
931 if (error)
932 goto out_end_trans;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100933 ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000934 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500935 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000936 brelse(dibh);
937 }
938
939 error = gfs2_dir_del(odip, &odentry->d_name);
940 if (error)
941 goto out_end_trans;
942
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100943 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000944 if (error)
945 goto out_end_trans;
946
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_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000950 if (alloc_required)
951 gfs2_inplace_release(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400952out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000953 if (alloc_required)
954 gfs2_quota_unlock(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400955out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000956 if (alloc_required)
957 gfs2_alloc_put(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400958out_gunlock:
Bob Peterson72dbf472008-08-12 13:39:29 -0500959 while (x--) {
960 gfs2_glock_dq(ghs + x);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000961 gfs2_holder_uninit(ghs + x);
Bob Peterson72dbf472008-08-12 13:39:29 -0500962 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400963out_gunlock_r:
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100964 if (r_gh.gh_gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000965 gfs2_glock_dq_uninit(&r_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400966out:
Bob Peterson46290342010-09-30 10:34:00 -0400967 gfs2_glock_dq_uninit(&ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000968 return error;
969}
970
971/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000972 * gfs2_follow_link - Follow a symbolic link
973 * @dentry: The dentry of the link
974 * @nd: Data that we pass to vfs_follow_link()
975 *
Al Viroc177c2a2010-01-14 00:59:16 -0500976 * This can handle symlinks of any size.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000977 *
978 * Returns: 0 on success or error code
979 */
980
981static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
982{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400983 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
Al Viroc177c2a2010-01-14 00:59:16 -0500984 struct gfs2_holder i_gh;
985 struct buffer_head *dibh;
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100986 unsigned int x, size;
Al Viroc177c2a2010-01-14 00:59:16 -0500987 char *buf;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000988 int error;
989
Al Viroc177c2a2010-01-14 00:59:16 -0500990 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
991 error = gfs2_glock_nq(&i_gh);
992 if (error) {
993 gfs2_holder_uninit(&i_gh);
994 nd_set_link(nd, ERR_PTR(error));
995 return NULL;
996 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000997
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100998 size = (unsigned int)i_size_read(&ip->i_inode);
999 if (size == 0) {
Al Viroc177c2a2010-01-14 00:59:16 -05001000 gfs2_consist_inode(ip);
1001 buf = ERR_PTR(-EIO);
1002 goto out;
1003 }
1004
1005 error = gfs2_meta_inode_buffer(ip, &dibh);
1006 if (error) {
1007 buf = ERR_PTR(error);
1008 goto out;
1009 }
1010
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001011 x = size + 1;
Al Viroc177c2a2010-01-14 00:59:16 -05001012 buf = kmalloc(x, GFP_NOFS);
1013 if (!buf)
1014 buf = ERR_PTR(-ENOMEM);
1015 else
1016 memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
1017 brelse(dibh);
1018out:
1019 gfs2_glock_dq_uninit(&i_gh);
1020 nd_set_link(nd, buf);
1021 return NULL;
1022}
1023
1024static void gfs2_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
1025{
1026 char *s = nd_get_link(nd);
1027 if (!IS_ERR(s))
1028 kfree(s);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001029}
1030
1031/**
1032 * gfs2_permission -
1033 * @inode:
1034 * @mask:
1035 * @nd: passed from Linux VFS, ignored by us
1036 *
Steven Whitehouse300c7d72006-11-27 09:55:28 -05001037 * This may be called from the VFS directly, or from within GFS2 with the
1038 * inode locked, so we look to see if the glock is already locked and only
1039 * lock the glock if its not already been done.
1040 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001041 * Returns: errno
1042 */
1043
Miklos Szeredif58ba882008-07-02 21:12:01 +02001044int gfs2_permission(struct inode *inode, int mask)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001045{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001046 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001047 struct gfs2_holder i_gh;
1048 int error;
Steven Whitehouse300c7d72006-11-27 09:55:28 -05001049 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001050
Steven Whitehouse7afd88d2008-02-22 16:07:18 +00001051 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Steven Whitehouse300c7d72006-11-27 09:55:28 -05001052 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
1053 if (error)
1054 return error;
1055 unlock = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001056 }
1057
Miklos Szeredif58ba882008-07-02 21:12:01 +02001058 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
1059 error = -EACCES;
1060 else
1061 error = generic_permission(inode, mask, gfs2_check_acl);
Steven Whitehouse300c7d72006-11-27 09:55:28 -05001062 if (unlock)
1063 gfs2_glock_dq_uninit(&i_gh);
1064
David Teiglandb3b94fa2006-01-16 16:50:04 +00001065 return error;
1066}
1067
David Teiglandb3b94fa2006-01-16 16:50:04 +00001068static int setattr_chown(struct inode *inode, struct iattr *attr)
1069{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001070 struct gfs2_inode *ip = GFS2_I(inode);
1071 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001072 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -04001073 u32 ouid, ogid, nuid, ngid;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001074 int error;
1075
Steven Whitehouse2933f922006-11-01 13:23:29 -05001076 ouid = inode->i_uid;
1077 ogid = inode->i_gid;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001078 nuid = attr->ia_uid;
1079 ngid = attr->ia_gid;
1080
1081 if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
1082 ouid = nuid = NO_QUOTA_CHANGE;
1083 if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
1084 ogid = ngid = NO_QUOTA_CHANGE;
1085
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +03001086 if (!gfs2_alloc_get(ip))
1087 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001088
1089 error = gfs2_quota_lock(ip, nuid, ngid);
1090 if (error)
1091 goto out_alloc;
1092
1093 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1094 error = gfs2_quota_check(ip, nuid, ngid);
1095 if (error)
1096 goto out_gunlock_q;
1097 }
1098
1099 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
1100 if (error)
1101 goto out_gunlock_q;
1102
1103 error = gfs2_meta_inode_buffer(ip, &dibh);
1104 if (error)
1105 goto out_end_trans;
1106
Christoph Hellwig10257742010-06-04 11:30:02 +02001107 if ((attr->ia_valid & ATTR_SIZE) &&
1108 attr->ia_size != i_size_read(inode)) {
1109 int error;
1110
1111 error = vmtruncate(inode, attr->ia_size);
1112 gfs2_assert_warn(sdp, !error);
1113 }
1114
1115 setattr_copy(inode, attr);
1116 mark_inode_dirty(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001117
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001118 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001119 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001120 brelse(dibh);
1121
1122 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001123 u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
1124 gfs2_quota_change(ip, -blocks, ouid, ogid);
1125 gfs2_quota_change(ip, blocks, nuid, ngid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001126 }
1127
Steven Whitehousea91ea692006-09-04 12:04:26 -04001128out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001129 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001130out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001131 gfs2_quota_unlock(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001132out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001133 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001134 return error;
1135}
1136
1137/**
1138 * gfs2_setattr - Change attributes on an inode
1139 * @dentry: The dentry which is changing
1140 * @attr: The structure describing the change
1141 *
1142 * The VFS layer wants to change one or more of an inodes attributes. Write
1143 * that change out to disk.
1144 *
1145 * Returns: errno
1146 */
1147
1148static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
1149{
1150 struct inode *inode = dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001151 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001152 struct gfs2_holder i_gh;
1153 int error;
1154
David Teiglandb3b94fa2006-01-16 16:50:04 +00001155 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1156 if (error)
1157 return error;
1158
1159 error = -EPERM;
1160 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1161 goto out;
1162
1163 error = inode_change_ok(inode, attr);
1164 if (error)
1165 goto out;
1166
1167 if (attr->ia_valid & ATTR_SIZE)
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001168 error = gfs2_setattr_size(inode, attr->ia_size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001169 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1170 error = setattr_chown(inode, attr);
1171 else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1172 error = gfs2_acl_chmod(ip, attr);
1173 else
1174 error = gfs2_setattr_simple(ip, attr);
1175
Steven Whitehousea91ea692006-09-04 12:04:26 -04001176out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001177 gfs2_glock_dq_uninit(&i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001178 if (!error)
1179 mark_inode_dirty(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001180 return error;
1181}
1182
1183/**
1184 * gfs2_getattr - Read out an inode's attributes
Steven Whitehouse26c1a572006-09-04 15:32:10 -04001185 * @mnt: The vfsmount the inode is being accessed from
David Teiglandb3b94fa2006-01-16 16:50:04 +00001186 * @dentry: The dentry to stat
1187 * @stat: The inode's stats
1188 *
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001189 * This may be called from the VFS directly, or from within GFS2 with the
1190 * inode locked, so we look to see if the glock is already locked and only
1191 * lock the glock if its not already been done. Note that its the NFS
1192 * readdirplus operation which causes this to be called (from filldir)
1193 * with the glock already held.
1194 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001195 * Returns: errno
1196 */
1197
1198static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1199 struct kstat *stat)
1200{
1201 struct inode *inode = dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001202 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001203 struct gfs2_holder gh;
1204 int error;
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001205 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001206
Steven Whitehouse7afd88d2008-02-22 16:07:18 +00001207 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001208 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1209 if (error)
1210 return error;
1211 unlock = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001212 }
1213
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001214 generic_fillattr(inode, stat);
Steven Whitehoused7c103d2007-01-25 17:14:59 +00001215 if (unlock)
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001216 gfs2_glock_dq_uninit(&gh);
1217
1218 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001219}
1220
1221static int gfs2_setxattr(struct dentry *dentry, const char *name,
1222 const void *data, size_t size, int flags)
1223{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001224 struct inode *inode = dentry->d_inode;
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001225 struct gfs2_inode *ip = GFS2_I(inode);
1226 struct gfs2_holder gh;
1227 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001228
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001229 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1230 ret = gfs2_glock_nq(&gh);
1231 if (ret == 0) {
1232 ret = generic_setxattr(dentry, name, data, size, flags);
1233 gfs2_glock_dq(&gh);
1234 }
1235 gfs2_holder_uninit(&gh);
1236 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001237}
1238
1239static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1240 void *data, size_t size)
1241{
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001242 struct inode *inode = dentry->d_inode;
1243 struct gfs2_inode *ip = GFS2_I(inode);
1244 struct gfs2_holder gh;
1245 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001246
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001247 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1248 ret = gfs2_glock_nq(&gh);
1249 if (ret == 0) {
1250 ret = generic_getxattr(dentry, name, data, size);
1251 gfs2_glock_dq(&gh);
1252 }
1253 gfs2_holder_uninit(&gh);
1254 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001255}
1256
1257static int gfs2_removexattr(struct dentry *dentry, const char *name)
1258{
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001259 struct inode *inode = dentry->d_inode;
1260 struct gfs2_inode *ip = GFS2_I(inode);
1261 struct gfs2_holder gh;
1262 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001263
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001264 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1265 ret = gfs2_glock_nq(&gh);
1266 if (ret == 0) {
1267 ret = generic_removexattr(dentry, name);
1268 gfs2_glock_dq(&gh);
1269 }
1270 gfs2_holder_uninit(&gh);
1271 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001272}
1273
Benjamin Marzinski39211202010-08-20 00:21:02 -05001274static void empty_write_end(struct page *page, unsigned from,
1275 unsigned to)
1276{
1277 struct gfs2_inode *ip = GFS2_I(page->mapping->host);
1278
1279 page_zero_new_buffers(page, from, to);
1280 flush_dcache_page(page);
1281 mark_page_accessed(page);
1282
1283 if (!gfs2_is_writeback(ip))
1284 gfs2_page_add_databufs(ip, page, from, to);
1285
1286 block_commit_write(page, from, to);
1287}
1288
1289
1290static int write_empty_blocks(struct page *page, unsigned from, unsigned to)
1291{
1292 unsigned start, end, next;
1293 struct buffer_head *bh, *head;
1294 int error;
1295
1296 if (!page_has_buffers(page)) {
Christoph Hellwigebdec242010-10-06 10:47:23 +02001297 error = __block_write_begin(page, from, to - from, gfs2_block_map);
Benjamin Marzinski39211202010-08-20 00:21:02 -05001298 if (unlikely(error))
1299 return error;
1300
1301 empty_write_end(page, from, to);
1302 return 0;
1303 }
1304
1305 bh = head = page_buffers(page);
1306 next = end = 0;
1307 while (next < from) {
1308 next += bh->b_size;
1309 bh = bh->b_this_page;
1310 }
1311 start = next;
1312 do {
1313 next += bh->b_size;
1314 if (buffer_mapped(bh)) {
1315 if (end) {
Christoph Hellwigebdec242010-10-06 10:47:23 +02001316 error = __block_write_begin(page, start, end - start,
Benjamin Marzinski39211202010-08-20 00:21:02 -05001317 gfs2_block_map);
1318 if (unlikely(error))
1319 return error;
1320 empty_write_end(page, start, end);
1321 end = 0;
1322 }
1323 start = next;
1324 }
1325 else
1326 end = next;
1327 bh = bh->b_this_page;
1328 } while (next < to);
1329
1330 if (end) {
Christoph Hellwigebdec242010-10-06 10:47:23 +02001331 error = __block_write_begin(page, start, end - start, gfs2_block_map);
Benjamin Marzinski39211202010-08-20 00:21:02 -05001332 if (unlikely(error))
1333 return error;
1334 empty_write_end(page, start, end);
1335 }
1336
1337 return 0;
1338}
1339
1340static int fallocate_chunk(struct inode *inode, loff_t offset, loff_t len,
1341 int mode)
1342{
1343 struct gfs2_inode *ip = GFS2_I(inode);
1344 struct buffer_head *dibh;
1345 int error;
1346 u64 start = offset >> PAGE_CACHE_SHIFT;
1347 unsigned int start_offset = offset & ~PAGE_CACHE_MASK;
1348 u64 end = (offset + len - 1) >> PAGE_CACHE_SHIFT;
1349 pgoff_t curr;
1350 struct page *page;
1351 unsigned int end_offset = (offset + len) & ~PAGE_CACHE_MASK;
1352 unsigned int from, to;
1353
1354 if (!end_offset)
1355 end_offset = PAGE_CACHE_SIZE;
1356
1357 error = gfs2_meta_inode_buffer(ip, &dibh);
1358 if (unlikely(error))
1359 goto out;
1360
1361 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1362
1363 if (gfs2_is_stuffed(ip)) {
1364 error = gfs2_unstuff_dinode(ip, NULL);
1365 if (unlikely(error))
1366 goto out;
1367 }
1368
1369 curr = start;
1370 offset = start << PAGE_CACHE_SHIFT;
1371 from = start_offset;
1372 to = PAGE_CACHE_SIZE;
1373 while (curr <= end) {
1374 page = grab_cache_page_write_begin(inode->i_mapping, curr,
1375 AOP_FLAG_NOFS);
1376 if (unlikely(!page)) {
1377 error = -ENOMEM;
1378 goto out;
1379 }
1380
1381 if (curr == end)
1382 to = end_offset;
1383 error = write_empty_blocks(page, from, to);
1384 if (!error && offset + to > inode->i_size &&
1385 !(mode & FALLOC_FL_KEEP_SIZE)) {
1386 i_size_write(inode, offset + to);
1387 }
1388 unlock_page(page);
1389 page_cache_release(page);
1390 if (error)
1391 goto out;
1392 curr++;
1393 offset += PAGE_CACHE_SIZE;
1394 from = 0;
1395 }
1396
1397 gfs2_dinode_out(ip, dibh->b_data);
1398 mark_inode_dirty(inode);
1399
1400 brelse(dibh);
1401
1402out:
1403 return error;
1404}
1405
1406static void calc_max_reserv(struct gfs2_inode *ip, loff_t max, loff_t *len,
1407 unsigned int *data_blocks, unsigned int *ind_blocks)
1408{
1409 const struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1410 unsigned int max_blocks = ip->i_alloc->al_rgd->rd_free_clone;
1411 unsigned int tmp, max_data = max_blocks - 3 * (sdp->sd_max_height - 1);
1412
1413 for (tmp = max_data; tmp > sdp->sd_diptrs;) {
1414 tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
1415 max_data -= tmp;
1416 }
1417 /* This calculation isn't the exact reverse of gfs2_write_calc_reserve,
Steven Whitehousefe08d5a2010-08-23 11:54:45 +01001418 so it might end up with fewer data blocks */
Benjamin Marzinski39211202010-08-20 00:21:02 -05001419 if (max_data <= *data_blocks)
1420 return;
1421 *data_blocks = max_data;
1422 *ind_blocks = max_blocks - max_data;
1423 *len = ((loff_t)max_data - 3) << sdp->sd_sb.sb_bsize_shift;
1424 if (*len > max) {
1425 *len = max;
1426 gfs2_write_calc_reserv(ip, max, data_blocks, ind_blocks);
1427 }
1428}
1429
1430static long gfs2_fallocate(struct inode *inode, int mode, loff_t offset,
1431 loff_t len)
1432{
1433 struct gfs2_sbd *sdp = GFS2_SB(inode);
1434 struct gfs2_inode *ip = GFS2_I(inode);
1435 unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
1436 loff_t bytes, max_bytes;
1437 struct gfs2_alloc *al;
1438 int error;
1439 loff_t next = (offset + len - 1) >> sdp->sd_sb.sb_bsize_shift;
1440 next = (next + 1) << sdp->sd_sb.sb_bsize_shift;
1441
1442 offset = (offset >> sdp->sd_sb.sb_bsize_shift) <<
1443 sdp->sd_sb.sb_bsize_shift;
1444
1445 len = next - offset;
1446 bytes = sdp->sd_max_rg_data * sdp->sd_sb.sb_bsize / 2;
1447 if (!bytes)
1448 bytes = UINT_MAX;
1449
1450 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &ip->i_gh);
1451 error = gfs2_glock_nq(&ip->i_gh);
1452 if (unlikely(error))
1453 goto out_uninit;
1454
1455 if (!gfs2_write_alloc_required(ip, offset, len))
1456 goto out_unlock;
1457
1458 while (len > 0) {
1459 if (len < bytes)
1460 bytes = len;
1461 al = gfs2_alloc_get(ip);
1462 if (!al) {
1463 error = -ENOMEM;
1464 goto out_unlock;
1465 }
1466
1467 error = gfs2_quota_lock_check(ip);
1468 if (error)
1469 goto out_alloc_put;
1470
1471retry:
1472 gfs2_write_calc_reserv(ip, bytes, &data_blocks, &ind_blocks);
1473
1474 al->al_requested = data_blocks + ind_blocks;
1475 error = gfs2_inplace_reserve(ip);
1476 if (error) {
1477 if (error == -ENOSPC && bytes > sdp->sd_sb.sb_bsize) {
1478 bytes >>= 1;
1479 goto retry;
1480 }
1481 goto out_qunlock;
1482 }
1483 max_bytes = bytes;
1484 calc_max_reserv(ip, len, &max_bytes, &data_blocks, &ind_blocks);
1485 al->al_requested = data_blocks + ind_blocks;
1486
1487 rblocks = RES_DINODE + ind_blocks + RES_STATFS + RES_QUOTA +
Benjamin Marzinskibf97b672010-09-27 16:00:04 -05001488 RES_RG_HDR + gfs2_rg_blocks(al);
Benjamin Marzinski39211202010-08-20 00:21:02 -05001489 if (gfs2_is_jdata(ip))
1490 rblocks += data_blocks ? data_blocks : 1;
1491
1492 error = gfs2_trans_begin(sdp, rblocks,
1493 PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize);
1494 if (error)
1495 goto out_trans_fail;
1496
1497 error = fallocate_chunk(inode, offset, max_bytes, mode);
1498 gfs2_trans_end(sdp);
1499
1500 if (error)
1501 goto out_trans_fail;
1502
1503 len -= max_bytes;
1504 offset += max_bytes;
1505 gfs2_inplace_release(ip);
1506 gfs2_quota_unlock(ip);
1507 gfs2_alloc_put(ip);
1508 }
1509 goto out_unlock;
1510
1511out_trans_fail:
1512 gfs2_inplace_release(ip);
1513out_qunlock:
1514 gfs2_quota_unlock(ip);
1515out_alloc_put:
1516 gfs2_alloc_put(ip);
1517out_unlock:
1518 gfs2_glock_dq(&ip->i_gh);
1519out_uninit:
1520 gfs2_holder_uninit(&ip->i_gh);
1521 return error;
1522}
1523
1524
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001525static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1526 u64 start, u64 len)
1527{
1528 struct gfs2_inode *ip = GFS2_I(inode);
1529 struct gfs2_holder gh;
1530 int ret;
1531
1532 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1533 if (ret)
1534 return ret;
1535
1536 mutex_lock(&inode->i_mutex);
1537
1538 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
1539 if (ret)
1540 goto out;
1541
1542 if (gfs2_is_stuffed(ip)) {
1543 u64 phys = ip->i_no_addr << inode->i_blkbits;
1544 u64 size = i_size_read(inode);
1545 u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
1546 FIEMAP_EXTENT_DATA_INLINE;
1547 phys += sizeof(struct gfs2_dinode);
1548 phys += start;
1549 if (start + len > size)
1550 len = size - start;
1551 if (start < size)
1552 ret = fiemap_fill_next_extent(fieinfo, start, phys,
1553 len, flags);
1554 if (ret == 1)
1555 ret = 0;
1556 } else {
1557 ret = __generic_block_fiemap(inode, fieinfo, start, len,
1558 gfs2_block_map);
1559 }
1560
1561 gfs2_glock_dq_uninit(&gh);
1562out:
1563 mutex_unlock(&inode->i_mutex);
1564 return ret;
1565}
1566
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001567const struct inode_operations gfs2_file_iops = {
Al Viroe6305c42008-07-15 21:03:57 -04001568 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001569 .setattr = gfs2_setattr,
1570 .getattr = gfs2_getattr,
1571 .setxattr = gfs2_setxattr,
1572 .getxattr = gfs2_getxattr,
1573 .listxattr = gfs2_listxattr,
1574 .removexattr = gfs2_removexattr,
Benjamin Marzinski39211202010-08-20 00:21:02 -05001575 .fallocate = gfs2_fallocate,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001576 .fiemap = gfs2_fiemap,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001577};
1578
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001579const struct inode_operations gfs2_dir_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001580 .create = gfs2_create,
1581 .lookup = gfs2_lookup,
1582 .link = gfs2_link,
1583 .unlink = gfs2_unlink,
1584 .symlink = gfs2_symlink,
1585 .mkdir = gfs2_mkdir,
1586 .rmdir = gfs2_rmdir,
1587 .mknod = gfs2_mknod,
1588 .rename = gfs2_rename,
Al Viroe6305c42008-07-15 21:03:57 -04001589 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001590 .setattr = gfs2_setattr,
1591 .getattr = gfs2_getattr,
1592 .setxattr = gfs2_setxattr,
1593 .getxattr = gfs2_getxattr,
1594 .listxattr = gfs2_listxattr,
1595 .removexattr = gfs2_removexattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001596 .fiemap = gfs2_fiemap,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001597};
1598
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001599const struct inode_operations gfs2_symlink_iops = {
Al Viroc177c2a2010-01-14 00:59:16 -05001600 .readlink = generic_readlink,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001601 .follow_link = gfs2_follow_link,
Al Viroc177c2a2010-01-14 00:59:16 -05001602 .put_link = gfs2_put_link,
Al Viroe6305c42008-07-15 21:03:57 -04001603 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001604 .setattr = gfs2_setattr,
1605 .getattr = gfs2_getattr,
1606 .setxattr = gfs2_setxattr,
1607 .getxattr = gfs2_getxattr,
1608 .listxattr = gfs2_listxattr,
1609 .removexattr = gfs2_removexattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001610 .fiemap = gfs2_fiemap,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001611};
1612