blob: abd5429ae2856a9d275ca75bd9273598994e94f1 [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>
15#include <linux/utsname.h>
16#include <linux/mm.h>
17#include <linux/xattr.h>
18#include <linux/posix_acl.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050019#include <linux/gfs2_ondisk.h>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050020#include <linux/crc32.h>
Steven Whitehousee9079cc2008-10-14 14:43:29 +010021#include <linux/fiemap.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"
29#include "eaops.h"
30#include "eattr.h"
31#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 +
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100222 al->al_rgd->rd_length +
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) {
Steven Whitehouse29937ac2006-07-06 17:58:03 -0400258 atomic_inc(&inode->i_count);
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
265/**
266 * gfs2_unlink - Unlink a file
267 * @dir: The inode of the directory containing the file to unlink
268 * @dentry: The file itself
269 *
270 * Unlink a file. Call gfs2_unlinki()
271 *
272 * Returns: errno
273 */
274
275static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
276{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400277 struct gfs2_inode *dip = GFS2_I(dir);
278 struct gfs2_sbd *sdp = GFS2_SB(dir);
279 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
Russell Cattelanddee7602007-01-29 17:13:44 -0600280 struct gfs2_holder ghs[3];
281 struct gfs2_rgrpd *rgd;
282 struct gfs2_holder ri_gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000283 int error;
284
Russell Cattelanddee7602007-01-29 17:13:44 -0600285 error = gfs2_rindex_hold(sdp, &ri_gh);
286 if (error)
287 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000288
Russell Cattelanddee7602007-01-29 17:13:44 -0600289 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
290 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
291
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100292 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
Russell Cattelanddee7602007-01-29 17:13:44 -0600293 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
294
295
Steven Whitehouse8497a462007-08-26 14:23:56 +0100296 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000297 if (error)
Steven Whitehouse8497a462007-08-26 14:23:56 +0100298 goto out_parent;
299
300 error = gfs2_glock_nq(ghs + 1); /* child */
301 if (error)
302 goto out_child;
303
304 error = gfs2_glock_nq(ghs + 2); /* rgrp */
305 if (error)
306 goto out_rgrp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000307
308 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
309 if (error)
Bob Peterson72dbf472008-08-12 13:39:29 -0500310 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000311
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400312 error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000313 if (error)
Steven Whitehouse8497a462007-08-26 14:23:56 +0100314 goto out_rgrp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000315
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400316 error = gfs2_dir_del(dip, &dentry->d_name);
317 if (error)
318 goto out_end_trans;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000319
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400320 error = gfs2_change_nlink(ip, -1);
321
322out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000323 gfs2_trans_end(sdp);
Bob Peterson72dbf472008-08-12 13:39:29 -0500324out_gunlock:
Steven Whitehouse8497a462007-08-26 14:23:56 +0100325 gfs2_glock_dq(ghs + 2);
326out_rgrp:
Russell Cattelanddee7602007-01-29 17:13:44 -0600327 gfs2_holder_uninit(ghs + 2);
Steven Whitehouse8497a462007-08-26 14:23:56 +0100328 gfs2_glock_dq(ghs + 1);
329out_child:
330 gfs2_holder_uninit(ghs + 1);
331 gfs2_glock_dq(ghs);
332out_parent:
333 gfs2_holder_uninit(ghs);
Russell Cattelanddee7602007-01-29 17:13:44 -0600334 gfs2_glock_dq_uninit(&ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000335 return error;
336}
337
338/**
339 * gfs2_symlink - Create a symlink
340 * @dir: The directory to create the symlink in
341 * @dentry: The dentry to put the symlink in
342 * @symname: The thing which the link points to
343 *
344 * Returns: errno
345 */
346
347static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
348 const char *symname)
349{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400350 struct gfs2_inode *dip = GFS2_I(dir), *ip;
351 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000352 struct gfs2_holder ghs[2];
353 struct inode *inode;
354 struct buffer_head *dibh;
355 int size;
356 int error;
357
David Teiglandb3b94fa2006-01-16 16:50:04 +0000358 /* Must be stuffed with a null terminator for gfs2_follow_link() */
359 size = strlen(symname);
360 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
361 return -ENAMETOOLONG;
362
363 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
364
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500365 inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000366 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000367 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000368 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000369 }
370
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500371 ip = ghs[1].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000372
Steven Whitehousec9e98882008-11-04 09:47:33 +0000373 ip->i_disksize = size;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000374
375 error = gfs2_meta_inode_buffer(ip, &dibh);
376
377 if (!gfs2_assert_withdraw(sdp, !error)) {
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500378 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000379 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
380 size);
381 brelse(dibh);
382 }
383
384 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000385 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000386 gfs2_inplace_release(dip);
387 gfs2_quota_unlock(dip);
388 gfs2_alloc_put(dip);
389
390 gfs2_glock_dq_uninit_m(2, ghs);
391
David Teiglandb3b94fa2006-01-16 16:50:04 +0000392 d_instantiate(dentry, inode);
393 mark_inode_dirty(inode);
394
395 return 0;
396}
397
398/**
399 * gfs2_mkdir - Make a directory
400 * @dir: The parent directory of the new one
401 * @dentry: The dentry of the new directory
402 * @mode: The mode of the new directory
403 *
404 * Returns: errno
405 */
406
407static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
408{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400409 struct gfs2_inode *dip = GFS2_I(dir), *ip;
410 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000411 struct gfs2_holder ghs[2];
412 struct inode *inode;
413 struct buffer_head *dibh;
414 int error;
415
David Teiglandb3b94fa2006-01-16 16:50:04 +0000416 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
417
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500418 inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000419 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000420 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000421 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000422 }
423
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500424 ip = ghs[1].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000425
Steven Whitehouse4f561102006-11-01 14:04:17 -0500426 ip->i_inode.i_nlink = 2;
Steven Whitehousec9e98882008-11-04 09:47:33 +0000427 ip->i_disksize = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000428 ip->i_diskflags |= GFS2_DIF_JDATA;
Steven Whitehousead6203f2008-11-03 13:59:19 +0000429 ip->i_entries = 2;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000430
431 error = gfs2_meta_inode_buffer(ip, &dibh);
432
433 if (!gfs2_assert_withdraw(sdp, !error)) {
434 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
Steven Whitehousec7526662006-03-20 12:30:04 -0500435 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500436 struct qstr str;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000437
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500438 gfs2_str2qstr(&str, ".");
Steven Whitehousec7526662006-03-20 12:30:04 -0500439 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
440 gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000441 dent->de_inum = di->di_num; /* already GFS2 endian */
Steven Whitehouse7ecdb702006-10-03 21:03:35 -0400442 dent->de_type = cpu_to_be16(DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000443 di->di_entries = cpu_to_be32(1);
444
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500445 gfs2_str2qstr(&str, "..");
Steven Whitehousec7526662006-03-20 12:30:04 -0500446 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
447 gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000448
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100449 gfs2_inum_out(dip, dent);
Steven Whitehouse7ecdb702006-10-03 21:03:35 -0400450 dent->de_type = cpu_to_be16(DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000451
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500452 gfs2_dinode_out(ip, di);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000453
454 brelse(dibh);
455 }
456
457 error = gfs2_change_nlink(dip, +1);
458 gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
459
460 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000461 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000462 gfs2_inplace_release(dip);
463 gfs2_quota_unlock(dip);
464 gfs2_alloc_put(dip);
465
466 gfs2_glock_dq_uninit_m(2, ghs);
467
David Teiglandb3b94fa2006-01-16 16:50:04 +0000468 d_instantiate(dentry, inode);
469 mark_inode_dirty(inode);
470
471 return 0;
472}
473
474/**
475 * gfs2_rmdir - Remove a directory
476 * @dir: The parent directory of the directory to be removed
477 * @dentry: The dentry of the directory to remove
478 *
479 * Remove a directory. Call gfs2_rmdiri()
480 *
481 * Returns: errno
482 */
483
484static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
485{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400486 struct gfs2_inode *dip = GFS2_I(dir);
487 struct gfs2_sbd *sdp = GFS2_SB(dir);
488 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
Russell Cattelanddee7602007-01-29 17:13:44 -0600489 struct gfs2_holder ghs[3];
490 struct gfs2_rgrpd *rgd;
491 struct gfs2_holder ri_gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000492 int error;
493
Russell Cattelanddee7602007-01-29 17:13:44 -0600494 error = gfs2_rindex_hold(sdp, &ri_gh);
495 if (error)
496 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000497 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
498 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
499
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100500 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
Russell Cattelanddee7602007-01-29 17:13:44 -0600501 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
502
Bob Peterson72dbf472008-08-12 13:39:29 -0500503 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000504 if (error)
Bob Peterson72dbf472008-08-12 13:39:29 -0500505 goto out_parent;
506
507 error = gfs2_glock_nq(ghs + 1); /* child */
508 if (error)
509 goto out_child;
510
511 error = gfs2_glock_nq(ghs + 2); /* rgrp */
512 if (error)
513 goto out_rgrp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000514
515 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
516 if (error)
517 goto out_gunlock;
518
Steven Whitehousead6203f2008-11-03 13:59:19 +0000519 if (ip->i_entries < 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000520 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500521 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000522 error = -EIO;
523 goto out_gunlock;
524 }
Steven Whitehousead6203f2008-11-03 13:59:19 +0000525 if (ip->i_entries > 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000526 error = -ENOTEMPTY;
527 goto out_gunlock;
528 }
529
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400530 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000531 if (error)
532 goto out_gunlock;
533
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400534 error = gfs2_rmdiri(dip, &dentry->d_name, ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000535
536 gfs2_trans_end(sdp);
537
Steven Whitehousea91ea692006-09-04 12:04:26 -0400538out_gunlock:
Bob Peterson72dbf472008-08-12 13:39:29 -0500539 gfs2_glock_dq(ghs + 2);
540out_rgrp:
Russell Cattelanddee7602007-01-29 17:13:44 -0600541 gfs2_holder_uninit(ghs + 2);
Bob Peterson72dbf472008-08-12 13:39:29 -0500542 gfs2_glock_dq(ghs + 1);
543out_child:
544 gfs2_holder_uninit(ghs + 1);
545 gfs2_glock_dq(ghs);
546out_parent:
547 gfs2_holder_uninit(ghs);
Russell Cattelanddee7602007-01-29 17:13:44 -0600548 gfs2_glock_dq_uninit(&ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000549 return error;
550}
551
552/**
553 * gfs2_mknod - Make a special file
554 * @dir: The directory in which the special file will reside
555 * @dentry: The dentry of the special file
556 * @mode: The mode of the special file
557 * @rdev: The device specification of the special file
558 *
559 */
560
561static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
562 dev_t dev)
563{
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500564 struct gfs2_inode *dip = GFS2_I(dir);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400565 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000566 struct gfs2_holder ghs[2];
567 struct inode *inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000568
569 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
570
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500571 inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000572 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000573 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000574 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000575 }
576
David Teiglandb3b94fa2006-01-16 16:50:04 +0000577 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000578 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000579 gfs2_inplace_release(dip);
580 gfs2_quota_unlock(dip);
581 gfs2_alloc_put(dip);
582
583 gfs2_glock_dq_uninit_m(2, ghs);
584
David Teiglandb3b94fa2006-01-16 16:50:04 +0000585 d_instantiate(dentry, inode);
586 mark_inode_dirty(inode);
587
588 return 0;
589}
590
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100591/*
592 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
593 * @this: move this
594 * @to: to here
595 *
596 * Follow @to back to the root and make sure we don't encounter @this
597 * Assumes we already hold the rename lock.
598 *
599 * Returns: errno
600 */
601
602static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
603{
604 struct inode *dir = &to->i_inode;
605 struct super_block *sb = dir->i_sb;
606 struct inode *tmp;
607 struct qstr dotdot;
608 int error = 0;
609
610 gfs2_str2qstr(&dotdot, "..");
611
612 igrab(dir);
613
614 for (;;) {
615 if (dir == &this->i_inode) {
616 error = -EINVAL;
617 break;
618 }
619 if (dir == sb->s_root->d_inode) {
620 error = 0;
621 break;
622 }
623
624 tmp = gfs2_lookupi(dir, &dotdot, 1);
625 if (IS_ERR(tmp)) {
626 error = PTR_ERR(tmp);
627 break;
628 }
629
630 iput(dir);
631 dir = tmp;
632 }
633
634 iput(dir);
635
636 return error;
637}
638
David Teiglandb3b94fa2006-01-16 16:50:04 +0000639/**
640 * gfs2_rename - Rename a file
641 * @odir: Parent directory of old file name
642 * @odentry: The old dentry of the file
643 * @ndir: Parent directory of new file name
644 * @ndentry: The new dentry of the file
645 *
646 * Returns: errno
647 */
648
649static int gfs2_rename(struct inode *odir, struct dentry *odentry,
650 struct inode *ndir, struct dentry *ndentry)
651{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400652 struct gfs2_inode *odip = GFS2_I(odir);
653 struct gfs2_inode *ndip = GFS2_I(ndir);
654 struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000655 struct gfs2_inode *nip = NULL;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400656 struct gfs2_sbd *sdp = GFS2_SB(odir);
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100657 struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, };
Russell Cattelanddee7602007-01-29 17:13:44 -0600658 struct gfs2_rgrpd *nrgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000659 unsigned int num_gh;
660 int dir_rename = 0;
661 int alloc_required;
662 unsigned int x;
663 int error;
664
David Teiglandb3b94fa2006-01-16 16:50:04 +0000665 if (ndentry->d_inode) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400666 nip = GFS2_I(ndentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000667 if (ip == nip)
668 return 0;
669 }
670
David Teiglandb3b94fa2006-01-16 16:50:04 +0000671
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100672 if (odip != ndip) {
673 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
674 0, &r_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000675 if (error)
676 goto out;
677
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100678 if (S_ISDIR(ip->i_inode.i_mode)) {
679 dir_rename = 1;
680 /* don't move a dirctory into it's subdir */
681 error = gfs2_ok_to_move(ip, ndip);
682 if (error)
683 goto out_gunlock_r;
684 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000685 }
686
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400687 num_gh = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000688 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400689 if (odip != ndip) {
690 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
691 num_gh++;
692 }
693 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
694 num_gh++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000695
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400696 if (nip) {
697 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
698 num_gh++;
Russell Cattelanddee7602007-01-29 17:13:44 -0600699 /* grab the resource lock for unlink flag twiddling
700 * this is the case of the target file already existing
701 * so we unlink before doing the rename
702 */
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100703 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
Russell Cattelanddee7602007-01-29 17:13:44 -0600704 if (nrgd)
705 gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400706 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000707
Bob Peterson72dbf472008-08-12 13:39:29 -0500708 for (x = 0; x < num_gh; x++) {
709 error = gfs2_glock_nq(ghs + x);
710 if (error)
711 goto out_gunlock;
712 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000713
714 /* Check out the old directory */
715
716 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
717 if (error)
718 goto out_gunlock;
719
720 /* Check out the new directory */
721
722 if (nip) {
723 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
724 if (error)
725 goto out_gunlock;
726
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500727 if (S_ISDIR(nip->i_inode.i_mode)) {
Steven Whitehousead6203f2008-11-03 13:59:19 +0000728 if (nip->i_entries < 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000729 if (gfs2_consist_inode(nip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500730 gfs2_dinode_print(nip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000731 error = -EIO;
732 goto out_gunlock;
733 }
Steven Whitehousead6203f2008-11-03 13:59:19 +0000734 if (nip->i_entries > 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000735 error = -ENOTEMPTY;
736 goto out_gunlock;
737 }
738 }
739 } else {
Miklos Szeredif58ba882008-07-02 21:12:01 +0200740 error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000741 if (error)
742 goto out_gunlock;
743
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100744 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000745 switch (error) {
746 case -ENOENT:
747 error = 0;
748 break;
749 case 0:
750 error = -EEXIST;
751 default:
752 goto out_gunlock;
753 };
754
755 if (odip != ndip) {
Steven Whitehouse4f561102006-11-01 14:04:17 -0500756 if (!ndip->i_inode.i_nlink) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000757 error = -EINVAL;
758 goto out_gunlock;
759 }
Steven Whitehousead6203f2008-11-03 13:59:19 +0000760 if (ndip->i_entries == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000761 error = -EFBIG;
762 goto out_gunlock;
763 }
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500764 if (S_ISDIR(ip->i_inode.i_mode) &&
Steven Whitehouse4f561102006-11-01 14:04:17 -0500765 ndip->i_inode.i_nlink == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000766 error = -EMLINK;
767 goto out_gunlock;
768 }
769 }
770 }
771
772 /* Check out the dir to be renamed */
773
774 if (dir_rename) {
Miklos Szeredif58ba882008-07-02 21:12:01 +0200775 error = gfs2_permission(odentry->d_inode, MAY_WRITE);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000776 if (error)
777 goto out_gunlock;
778 }
779
Steven Whitehousec7526662006-03-20 12:30:04 -0500780 alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
781 if (error < 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000782 goto out_gunlock;
Steven Whitehousec7526662006-03-20 12:30:04 -0500783 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000784
785 if (alloc_required) {
786 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300787 if (!al) {
788 error = -ENOMEM;
789 goto out_gunlock;
790 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000791
Steven Whitehoused82661d2008-03-10 15:34:50 +0000792 error = gfs2_quota_lock_check(ndip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000793 if (error)
794 goto out_alloc;
795
David Teiglandb3b94fa2006-01-16 16:50:04 +0000796 al->al_requested = sdp->sd_max_dirres;
797
798 error = gfs2_inplace_reserve(ndip);
799 if (error)
800 goto out_gunlock_q;
801
Steven Whitehousefe1bded2006-04-18 10:09:15 -0400802 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100803 al->al_rgd->rd_length +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000804 4 * RES_DINODE + 4 * RES_LEAF +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500805 RES_STATFS + RES_QUOTA + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000806 if (error)
807 goto out_ipreserv;
808 } else {
809 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500810 5 * RES_LEAF + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000811 if (error)
812 goto out_gunlock;
813 }
814
815 /* Remove the target file, if it exists */
816
817 if (nip) {
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500818 if (S_ISDIR(nip->i_inode.i_mode))
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400819 error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
820 else {
821 error = gfs2_dir_del(ndip, &ndentry->d_name);
822 if (error)
823 goto out_end_trans;
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500824 error = gfs2_change_nlink(nip, -1);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400825 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000826 if (error)
827 goto out_end_trans;
828 }
829
830 if (dir_rename) {
831 struct qstr name;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500832 gfs2_str2qstr(&name, "..");
David Teiglandb3b94fa2006-01-16 16:50:04 +0000833
834 error = gfs2_change_nlink(ndip, +1);
835 if (error)
836 goto out_end_trans;
837 error = gfs2_change_nlink(odip, -1);
838 if (error)
839 goto out_end_trans;
840
Steven Whitehouseffed8ab2007-06-07 11:29:35 +0100841 error = gfs2_dir_mvino(ip, &name, ndip, DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000842 if (error)
843 goto out_end_trans;
844 } else {
845 struct buffer_head *dibh;
846 error = gfs2_meta_inode_buffer(ip, &dibh);
847 if (error)
848 goto out_end_trans;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100849 ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000850 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500851 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000852 brelse(dibh);
853 }
854
855 error = gfs2_dir_del(odip, &odentry->d_name);
856 if (error)
857 goto out_end_trans;
858
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100859 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000860 if (error)
861 goto out_end_trans;
862
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400863out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000864 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400865out_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000866 if (alloc_required)
867 gfs2_inplace_release(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400868out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000869 if (alloc_required)
870 gfs2_quota_unlock(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400871out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000872 if (alloc_required)
873 gfs2_alloc_put(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400874out_gunlock:
Bob Peterson72dbf472008-08-12 13:39:29 -0500875 while (x--) {
876 gfs2_glock_dq(ghs + x);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000877 gfs2_holder_uninit(ghs + x);
Bob Peterson72dbf472008-08-12 13:39:29 -0500878 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400879out_gunlock_r:
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100880 if (r_gh.gh_gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000881 gfs2_glock_dq_uninit(&r_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400882out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000883 return error;
884}
885
886/**
887 * gfs2_readlink - Read the value of a symlink
888 * @dentry: the symlink
889 * @buf: the buffer to read the symlink data into
890 * @size: the size of the buffer
891 *
892 * Returns: errno
893 */
894
895static int gfs2_readlink(struct dentry *dentry, char __user *user_buf,
896 int user_size)
897{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400898 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000899 char array[GFS2_FAST_NAME_SIZE], *buf = array;
900 unsigned int len = GFS2_FAST_NAME_SIZE;
901 int error;
902
David Teiglandb3b94fa2006-01-16 16:50:04 +0000903 error = gfs2_readlinki(ip, &buf, &len);
904 if (error)
905 return error;
906
907 if (user_size > len - 1)
908 user_size = len - 1;
909
910 if (copy_to_user(user_buf, buf, user_size))
911 error = -EFAULT;
912 else
913 error = user_size;
914
915 if (buf != array)
916 kfree(buf);
917
918 return error;
919}
920
921/**
922 * gfs2_follow_link - Follow a symbolic link
923 * @dentry: The dentry of the link
924 * @nd: Data that we pass to vfs_follow_link()
925 *
926 * This can handle symlinks of any size. It is optimised for symlinks
927 * under GFS2_FAST_NAME_SIZE.
928 *
929 * Returns: 0 on success or error code
930 */
931
932static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
933{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400934 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000935 char array[GFS2_FAST_NAME_SIZE], *buf = array;
936 unsigned int len = GFS2_FAST_NAME_SIZE;
937 int error;
938
David Teiglandb3b94fa2006-01-16 16:50:04 +0000939 error = gfs2_readlinki(ip, &buf, &len);
940 if (!error) {
941 error = vfs_follow_link(nd, buf);
942 if (buf != array)
943 kfree(buf);
944 }
945
946 return ERR_PTR(error);
947}
948
949/**
950 * gfs2_permission -
951 * @inode:
952 * @mask:
953 * @nd: passed from Linux VFS, ignored by us
954 *
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500955 * This may be called from the VFS directly, or from within GFS2 with the
956 * inode locked, so we look to see if the glock is already locked and only
957 * lock the glock if its not already been done.
958 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000959 * Returns: errno
960 */
961
Miklos Szeredif58ba882008-07-02 21:12:01 +0200962int gfs2_permission(struct inode *inode, int mask)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000963{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400964 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000965 struct gfs2_holder i_gh;
966 int error;
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500967 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000968
Steven Whitehouse7afd88d2008-02-22 16:07:18 +0000969 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500970 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
971 if (error)
972 return error;
973 unlock = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000974 }
975
Miklos Szeredif58ba882008-07-02 21:12:01 +0200976 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
977 error = -EACCES;
978 else
979 error = generic_permission(inode, mask, gfs2_check_acl);
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500980 if (unlock)
981 gfs2_glock_dq_uninit(&i_gh);
982
David Teiglandb3b94fa2006-01-16 16:50:04 +0000983 return error;
984}
985
986static int setattr_size(struct inode *inode, struct iattr *attr)
987{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400988 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100989 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000990 int error;
991
Steven Whitehousec9e98882008-11-04 09:47:33 +0000992 if (attr->ia_size != ip->i_disksize) {
Steven Whitehouse16615be2007-09-17 10:59:52 +0100993 error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000994 if (error)
995 return error;
Steven Whitehouse16615be2007-09-17 10:59:52 +0100996 error = vmtruncate(inode, attr->ia_size);
997 gfs2_trans_end(sdp);
998 if (error)
999 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001000 }
1001
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +00001002 error = gfs2_truncatei(ip, attr->ia_size);
Steven Whitehousec9e98882008-11-04 09:47:33 +00001003 if (error && (inode->i_size != ip->i_disksize))
1004 i_size_write(inode, ip->i_disksize);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001005
1006 return error;
1007}
1008
1009static int setattr_chown(struct inode *inode, struct iattr *attr)
1010{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001011 struct gfs2_inode *ip = GFS2_I(inode);
1012 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001013 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -04001014 u32 ouid, ogid, nuid, ngid;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001015 int error;
1016
Steven Whitehouse2933f922006-11-01 13:23:29 -05001017 ouid = inode->i_uid;
1018 ogid = inode->i_gid;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001019 nuid = attr->ia_uid;
1020 ngid = attr->ia_gid;
1021
1022 if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
1023 ouid = nuid = NO_QUOTA_CHANGE;
1024 if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
1025 ogid = ngid = NO_QUOTA_CHANGE;
1026
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +03001027 if (!gfs2_alloc_get(ip))
1028 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001029
1030 error = gfs2_quota_lock(ip, nuid, ngid);
1031 if (error)
1032 goto out_alloc;
1033
1034 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1035 error = gfs2_quota_check(ip, nuid, ngid);
1036 if (error)
1037 goto out_gunlock_q;
1038 }
1039
1040 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
1041 if (error)
1042 goto out_gunlock_q;
1043
1044 error = gfs2_meta_inode_buffer(ip, &dibh);
1045 if (error)
1046 goto out_end_trans;
1047
1048 error = inode_setattr(inode, attr);
1049 gfs2_assert_warn(sdp, !error);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001050
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001051 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001052 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001053 brelse(dibh);
1054
1055 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001056 u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
1057 gfs2_quota_change(ip, -blocks, ouid, ogid);
1058 gfs2_quota_change(ip, blocks, nuid, ngid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001059 }
1060
Steven Whitehousea91ea692006-09-04 12:04:26 -04001061out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001062 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001063out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001064 gfs2_quota_unlock(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001065out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001066 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001067 return error;
1068}
1069
1070/**
1071 * gfs2_setattr - Change attributes on an inode
1072 * @dentry: The dentry which is changing
1073 * @attr: The structure describing the change
1074 *
1075 * The VFS layer wants to change one or more of an inodes attributes. Write
1076 * that change out to disk.
1077 *
1078 * Returns: errno
1079 */
1080
1081static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
1082{
1083 struct inode *inode = dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001084 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001085 struct gfs2_holder i_gh;
1086 int error;
1087
David Teiglandb3b94fa2006-01-16 16:50:04 +00001088 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1089 if (error)
1090 return error;
1091
1092 error = -EPERM;
1093 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1094 goto out;
1095
1096 error = inode_change_ok(inode, attr);
1097 if (error)
1098 goto out;
1099
1100 if (attr->ia_valid & ATTR_SIZE)
1101 error = setattr_size(inode, attr);
1102 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1103 error = setattr_chown(inode, attr);
1104 else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1105 error = gfs2_acl_chmod(ip, attr);
1106 else
1107 error = gfs2_setattr_simple(ip, attr);
1108
Steven Whitehousea91ea692006-09-04 12:04:26 -04001109out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001110 gfs2_glock_dq_uninit(&i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001111 if (!error)
1112 mark_inode_dirty(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001113 return error;
1114}
1115
1116/**
1117 * gfs2_getattr - Read out an inode's attributes
Steven Whitehouse26c1a572006-09-04 15:32:10 -04001118 * @mnt: The vfsmount the inode is being accessed from
David Teiglandb3b94fa2006-01-16 16:50:04 +00001119 * @dentry: The dentry to stat
1120 * @stat: The inode's stats
1121 *
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001122 * This may be called from the VFS directly, or from within GFS2 with the
1123 * inode locked, so we look to see if the glock is already locked and only
1124 * lock the glock if its not already been done. Note that its the NFS
1125 * readdirplus operation which causes this to be called (from filldir)
1126 * with the glock already held.
1127 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001128 * Returns: errno
1129 */
1130
1131static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1132 struct kstat *stat)
1133{
1134 struct inode *inode = dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001135 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001136 struct gfs2_holder gh;
1137 int error;
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001138 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001139
Steven Whitehouse7afd88d2008-02-22 16:07:18 +00001140 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001141 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1142 if (error)
1143 return error;
1144 unlock = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001145 }
1146
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001147 generic_fillattr(inode, stat);
Steven Whitehoused7c103d2007-01-25 17:14:59 +00001148 if (unlock)
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001149 gfs2_glock_dq_uninit(&gh);
1150
1151 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001152}
1153
1154static int gfs2_setxattr(struct dentry *dentry, const char *name,
1155 const void *data, size_t size, int flags)
1156{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001157 struct inode *inode = dentry->d_inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001158 struct gfs2_ea_request er;
1159
David Teiglandb3b94fa2006-01-16 16:50:04 +00001160 memset(&er, 0, sizeof(struct gfs2_ea_request));
1161 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1162 if (er.er_type == GFS2_EATYPE_UNUSED)
1163 return -EOPNOTSUPP;
1164 er.er_data = (char *)data;
1165 er.er_name_len = strlen(er.er_name);
1166 er.er_data_len = size;
1167 er.er_flags = flags;
1168
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001169 gfs2_assert_warn(GFS2_SB(inode), !(er.er_flags & GFS2_ERF_MODE));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001170
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001171 return gfs2_ea_set(GFS2_I(inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001172}
1173
1174static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1175 void *data, size_t size)
1176{
1177 struct gfs2_ea_request er;
1178
David Teiglandb3b94fa2006-01-16 16:50:04 +00001179 memset(&er, 0, sizeof(struct gfs2_ea_request));
1180 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1181 if (er.er_type == GFS2_EATYPE_UNUSED)
1182 return -EOPNOTSUPP;
1183 er.er_data = data;
1184 er.er_name_len = strlen(er.er_name);
1185 er.er_data_len = size;
1186
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001187 return gfs2_ea_get(GFS2_I(dentry->d_inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001188}
1189
1190static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
1191{
1192 struct gfs2_ea_request er;
1193
David Teiglandb3b94fa2006-01-16 16:50:04 +00001194 memset(&er, 0, sizeof(struct gfs2_ea_request));
1195 er.er_data = (size) ? buffer : NULL;
1196 er.er_data_len = size;
1197
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001198 return gfs2_ea_list(GFS2_I(dentry->d_inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001199}
1200
1201static int gfs2_removexattr(struct dentry *dentry, const char *name)
1202{
1203 struct gfs2_ea_request er;
1204
David Teiglandb3b94fa2006-01-16 16:50:04 +00001205 memset(&er, 0, sizeof(struct gfs2_ea_request));
1206 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1207 if (er.er_type == GFS2_EATYPE_UNUSED)
1208 return -EOPNOTSUPP;
1209 er.er_name_len = strlen(er.er_name);
1210
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001211 return gfs2_ea_remove(GFS2_I(dentry->d_inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001212}
1213
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001214static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1215 u64 start, u64 len)
1216{
1217 struct gfs2_inode *ip = GFS2_I(inode);
1218 struct gfs2_holder gh;
1219 int ret;
1220
1221 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1222 if (ret)
1223 return ret;
1224
1225 mutex_lock(&inode->i_mutex);
1226
1227 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
1228 if (ret)
1229 goto out;
1230
1231 if (gfs2_is_stuffed(ip)) {
1232 u64 phys = ip->i_no_addr << inode->i_blkbits;
1233 u64 size = i_size_read(inode);
1234 u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
1235 FIEMAP_EXTENT_DATA_INLINE;
1236 phys += sizeof(struct gfs2_dinode);
1237 phys += start;
1238 if (start + len > size)
1239 len = size - start;
1240 if (start < size)
1241 ret = fiemap_fill_next_extent(fieinfo, start, phys,
1242 len, flags);
1243 if (ret == 1)
1244 ret = 0;
1245 } else {
1246 ret = __generic_block_fiemap(inode, fieinfo, start, len,
1247 gfs2_block_map);
1248 }
1249
1250 gfs2_glock_dq_uninit(&gh);
1251out:
1252 mutex_unlock(&inode->i_mutex);
1253 return ret;
1254}
1255
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001256const struct inode_operations gfs2_file_iops = {
Al Viroe6305c42008-07-15 21:03:57 -04001257 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001258 .setattr = gfs2_setattr,
1259 .getattr = gfs2_getattr,
1260 .setxattr = gfs2_setxattr,
1261 .getxattr = gfs2_getxattr,
1262 .listxattr = gfs2_listxattr,
1263 .removexattr = gfs2_removexattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001264 .fiemap = gfs2_fiemap,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001265};
1266
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001267const struct inode_operations gfs2_dir_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001268 .create = gfs2_create,
1269 .lookup = gfs2_lookup,
1270 .link = gfs2_link,
1271 .unlink = gfs2_unlink,
1272 .symlink = gfs2_symlink,
1273 .mkdir = gfs2_mkdir,
1274 .rmdir = gfs2_rmdir,
1275 .mknod = gfs2_mknod,
1276 .rename = gfs2_rename,
Al Viroe6305c42008-07-15 21:03:57 -04001277 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001278 .setattr = gfs2_setattr,
1279 .getattr = gfs2_getattr,
1280 .setxattr = gfs2_setxattr,
1281 .getxattr = gfs2_getxattr,
1282 .listxattr = gfs2_listxattr,
1283 .removexattr = gfs2_removexattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001284 .fiemap = gfs2_fiemap,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001285};
1286
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001287const struct inode_operations gfs2_symlink_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001288 .readlink = gfs2_readlink,
1289 .follow_link = gfs2_follow_link,
Al Viroe6305c42008-07-15 21:03:57 -04001290 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001291 .setattr = gfs2_setattr,
1292 .getattr = gfs2_getattr,
1293 .setxattr = gfs2_setxattr,
1294 .getxattr = gfs2_getxattr,
1295 .listxattr = gfs2_listxattr,
1296 .removexattr = gfs2_removexattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001297 .fiemap = gfs2_fiemap,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001298};
1299