blob: 60f47bf2e8e8cabf86c38d2e94c77c6a2893a72c [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
10#include <linux/sched.h>
11#include <linux/slab.h>
12#include <linux/spinlock.h>
13#include <linux/completion.h>
14#include <linux/buffer_head.h>
15#include <linux/namei.h>
16#include <linux/utsname.h>
17#include <linux/mm.h>
18#include <linux/xattr.h>
19#include <linux/posix_acl.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050020#include <linux/gfs2_ondisk.h>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050021#include <linux/crc32.h>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020022#include <linux/lm_interface.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"
30#include "eaops.h"
31#include "eattr.h"
32#include "glock.h"
33#include "inode.h"
34#include "meta_io.h"
35#include "ops_dentry.h"
36#include "ops_inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000037#include "quota.h"
38#include "rgrp.h"
39#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050040#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000041
42/**
43 * gfs2_create - Create a file
44 * @dir: The directory in which to create the file
45 * @dentry: The dentry of the new file
46 * @mode: The mode of the new file
47 *
48 * Returns: errno
49 */
50
51static int gfs2_create(struct inode *dir, struct dentry *dentry,
52 int mode, struct nameidata *nd)
53{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040054 struct gfs2_inode *dip = GFS2_I(dir);
55 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +000056 struct gfs2_holder ghs[2];
57 struct inode *inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +000058
David Teiglandb3b94fa2006-01-16 16:50:04 +000059 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
60
61 for (;;) {
Steven Whitehousee7f14f42006-10-31 21:45:08 -050062 inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +000063 if (!IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +000064 gfs2_trans_end(sdp);
65 if (dip->i_alloc.al_rgd)
66 gfs2_inplace_release(dip);
67 gfs2_quota_unlock(dip);
68 gfs2_alloc_put(dip);
69 gfs2_glock_dq_uninit_m(2, ghs);
Steven Whitehouse3a8476d2006-06-19 09:10:39 -040070 mark_inode_dirty(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000071 break;
Steven Whitehouse7359a192006-02-13 12:27:43 +000072 } else if (PTR_ERR(inode) != -EEXIST ||
David Teiglandb3b94fa2006-01-16 16:50:04 +000073 (nd->intent.open.flags & O_EXCL)) {
74 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +000075 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000076 }
77
Steven Whitehousec7526662006-03-20 12:30:04 -050078 inode = gfs2_lookupi(dir, &dentry->d_name, 0, nd);
79 if (inode) {
80 if (!IS_ERR(inode)) {
Steven Whitehousec7526662006-03-20 12:30:04 -050081 gfs2_holder_uninit(ghs);
82 break;
83 } else {
84 gfs2_holder_uninit(ghs);
85 return PTR_ERR(inode);
86 }
David Teiglandb3b94fa2006-01-16 16:50:04 +000087 }
88 }
89
David Teiglandb3b94fa2006-01-16 16:50:04 +000090 d_instantiate(dentry, inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000091
92 return 0;
93}
94
95/**
96 * gfs2_lookup - Look up a filename in a directory and return its inode
97 * @dir: The directory inode
98 * @dentry: The dentry of the new inode
99 * @nd: passed from Linux VFS, ignored by us
100 *
101 * Called by the VFS layer. Lock dir and call gfs2_lookupi()
102 *
103 * Returns: errno
104 */
105
106static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
107 struct nameidata *nd)
108{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000109 struct inode *inode = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000110
Steven Whitehousec7526662006-03-20 12:30:04 -0500111 dentry->d_op = &gfs2_dops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000112
Steven Whitehousec7526662006-03-20 12:30:04 -0500113 inode = gfs2_lookupi(dir, &dentry->d_name, 0, nd);
114 if (inode && IS_ERR(inode))
115 return ERR_PTR(PTR_ERR(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000116
117 if (inode)
118 return d_splice_alias(inode, dentry);
119 d_add(dentry, inode);
120
121 return NULL;
122}
123
124/**
125 * gfs2_link - Link to a file
126 * @old_dentry: The inode to link
127 * @dir: Add link to this directory
128 * @dentry: The name of the link
129 *
130 * Link the inode in "old_dentry" into the directory "dir" with the
131 * name in "dentry".
132 *
133 * Returns: errno
134 */
135
136static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
137 struct dentry *dentry)
138{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400139 struct gfs2_inode *dip = GFS2_I(dir);
140 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000141 struct inode *inode = old_dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400142 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000143 struct gfs2_holder ghs[2];
144 int alloc_required;
145 int error;
146
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500147 if (S_ISDIR(inode->i_mode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000148 return -EPERM;
149
150 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
151 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
152
153 error = gfs2_glock_nq_m(2, ghs);
154 if (error)
155 goto out;
156
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400157 error = permission(dir, MAY_WRITE | MAY_EXEC, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000158 if (error)
159 goto out_gunlock;
160
Steven Whitehousec7526662006-03-20 12:30:04 -0500161 error = gfs2_dir_search(dir, &dentry->d_name, NULL, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000162 switch (error) {
163 case -ENOENT:
164 break;
165 case 0:
166 error = -EEXIST;
167 default:
168 goto out_gunlock;
169 }
170
171 error = -EINVAL;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500172 if (!dip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000173 goto out_gunlock;
174 error = -EFBIG;
Steven Whitehousecd915492006-09-04 12:49:07 -0400175 if (dip->i_di.di_entries == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000176 goto out_gunlock;
177 error = -EPERM;
178 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
179 goto out_gunlock;
180 error = -EINVAL;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500181 if (!ip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000182 goto out_gunlock;
183 error = -EMLINK;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500184 if (ip->i_inode.i_nlink == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000185 goto out_gunlock;
186
Steven Whitehousec7526662006-03-20 12:30:04 -0500187 alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
188 if (error < 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000189 goto out_gunlock;
Steven Whitehousec7526662006-03-20 12:30:04 -0500190 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000191
192 if (alloc_required) {
193 struct gfs2_alloc *al = gfs2_alloc_get(dip);
194
195 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
196 if (error)
197 goto out_alloc;
198
Steven Whitehouse2933f922006-11-01 13:23:29 -0500199 error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000200 if (error)
201 goto out_gunlock_q;
202
203 al->al_requested = sdp->sd_max_dirres;
204
205 error = gfs2_inplace_reserve(dip);
206 if (error)
207 goto out_gunlock_q;
208
Steven Whitehouse1b502592006-05-18 14:10:52 -0400209 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000210 al->al_rgd->rd_ri.ri_length +
211 2 * RES_DINODE + RES_STATFS +
212 RES_QUOTA, 0);
213 if (error)
214 goto out_ipres;
215 } else {
216 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
217 if (error)
218 goto out_ipres;
219 }
220
Steven Whitehousec7526662006-03-20 12:30:04 -0500221 error = gfs2_dir_add(dir, &dentry->d_name, &ip->i_num,
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500222 IF2DT(inode->i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000223 if (error)
224 goto out_end_trans;
225
226 error = gfs2_change_nlink(ip, +1);
227
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400228out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000229 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400230out_ipres:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000231 if (alloc_required)
232 gfs2_inplace_release(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400233out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000234 if (alloc_required)
235 gfs2_quota_unlock(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400236out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000237 if (alloc_required)
238 gfs2_alloc_put(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400239out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000240 gfs2_glock_dq_m(2, ghs);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400241out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000242 gfs2_holder_uninit(ghs);
243 gfs2_holder_uninit(ghs + 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000244 if (!error) {
Steven Whitehouse29937ac2006-07-06 17:58:03 -0400245 atomic_inc(&inode->i_count);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000246 d_instantiate(dentry, inode);
247 mark_inode_dirty(inode);
248 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000249 return error;
250}
251
252/**
253 * gfs2_unlink - Unlink a file
254 * @dir: The inode of the directory containing the file to unlink
255 * @dentry: The file itself
256 *
257 * Unlink a file. Call gfs2_unlinki()
258 *
259 * Returns: errno
260 */
261
262static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
263{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400264 struct gfs2_inode *dip = GFS2_I(dir);
265 struct gfs2_sbd *sdp = GFS2_SB(dir);
266 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
Russell Cattelanddee7602007-01-29 17:13:44 -0600267 struct gfs2_holder ghs[3];
268 struct gfs2_rgrpd *rgd;
269 struct gfs2_holder ri_gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000270 int error;
271
Russell Cattelanddee7602007-01-29 17:13:44 -0600272 error = gfs2_rindex_hold(sdp, &ri_gh);
273 if (error)
274 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000275
Russell Cattelanddee7602007-01-29 17:13:44 -0600276 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
277 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
278
279 rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr);
280 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
281
282
283 error = gfs2_glock_nq_m(3, ghs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000284 if (error)
285 goto out;
286
287 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
288 if (error)
289 goto out_gunlock;
290
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400291 error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000292 if (error)
293 goto out_gunlock;
294
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400295 error = gfs2_dir_del(dip, &dentry->d_name);
296 if (error)
297 goto out_end_trans;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000298
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400299 error = gfs2_change_nlink(ip, -1);
300
301out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000302 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400303out_gunlock:
Russell Cattelanddee7602007-01-29 17:13:44 -0600304 gfs2_glock_dq_m(3, ghs);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400305out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000306 gfs2_holder_uninit(ghs);
307 gfs2_holder_uninit(ghs + 1);
Russell Cattelanddee7602007-01-29 17:13:44 -0600308 gfs2_holder_uninit(ghs + 2);
309 gfs2_glock_dq_uninit(&ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000310 return error;
311}
312
313/**
314 * gfs2_symlink - Create a symlink
315 * @dir: The directory to create the symlink in
316 * @dentry: The dentry to put the symlink in
317 * @symname: The thing which the link points to
318 *
319 * Returns: errno
320 */
321
322static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
323 const char *symname)
324{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400325 struct gfs2_inode *dip = GFS2_I(dir), *ip;
326 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000327 struct gfs2_holder ghs[2];
328 struct inode *inode;
329 struct buffer_head *dibh;
330 int size;
331 int error;
332
David Teiglandb3b94fa2006-01-16 16:50:04 +0000333 /* Must be stuffed with a null terminator for gfs2_follow_link() */
334 size = strlen(symname);
335 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
336 return -ENAMETOOLONG;
337
338 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
339
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500340 inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000341 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000342 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000343 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000344 }
345
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500346 ip = ghs[1].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000347
348 ip->i_di.di_size = size;
349
350 error = gfs2_meta_inode_buffer(ip, &dibh);
351
352 if (!gfs2_assert_withdraw(sdp, !error)) {
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500353 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000354 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
355 size);
356 brelse(dibh);
357 }
358
359 gfs2_trans_end(sdp);
360 if (dip->i_alloc.al_rgd)
361 gfs2_inplace_release(dip);
362 gfs2_quota_unlock(dip);
363 gfs2_alloc_put(dip);
364
365 gfs2_glock_dq_uninit_m(2, ghs);
366
David Teiglandb3b94fa2006-01-16 16:50:04 +0000367 d_instantiate(dentry, inode);
368 mark_inode_dirty(inode);
369
370 return 0;
371}
372
373/**
374 * gfs2_mkdir - Make a directory
375 * @dir: The parent directory of the new one
376 * @dentry: The dentry of the new directory
377 * @mode: The mode of the new directory
378 *
379 * Returns: errno
380 */
381
382static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
383{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400384 struct gfs2_inode *dip = GFS2_I(dir), *ip;
385 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000386 struct gfs2_holder ghs[2];
387 struct inode *inode;
388 struct buffer_head *dibh;
389 int error;
390
David Teiglandb3b94fa2006-01-16 16:50:04 +0000391 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
392
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500393 inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000394 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000395 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000396 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000397 }
398
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500399 ip = ghs[1].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000400
Steven Whitehouse4f561102006-11-01 14:04:17 -0500401 ip->i_inode.i_nlink = 2;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000402 ip->i_di.di_size = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
403 ip->i_di.di_flags |= GFS2_DIF_JDATA;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000404 ip->i_di.di_entries = 2;
405
406 error = gfs2_meta_inode_buffer(ip, &dibh);
407
408 if (!gfs2_assert_withdraw(sdp, !error)) {
409 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
Steven Whitehousec7526662006-03-20 12:30:04 -0500410 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500411 struct qstr str;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000412
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500413 gfs2_str2qstr(&str, ".");
Steven Whitehousec7526662006-03-20 12:30:04 -0500414 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
415 gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000416 dent->de_inum = di->di_num; /* already GFS2 endian */
Steven Whitehouse7ecdb702006-10-03 21:03:35 -0400417 dent->de_type = cpu_to_be16(DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000418 di->di_entries = cpu_to_be32(1);
419
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500420 gfs2_str2qstr(&str, "..");
Steven Whitehousec7526662006-03-20 12:30:04 -0500421 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
422 gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000423
Steven Whitehouse409e1852006-10-02 14:20:43 -0400424 gfs2_inum_out(&dip->i_num, &dent->de_inum);
Steven Whitehouse7ecdb702006-10-03 21:03:35 -0400425 dent->de_type = cpu_to_be16(DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000426
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500427 gfs2_dinode_out(ip, di);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000428
429 brelse(dibh);
430 }
431
432 error = gfs2_change_nlink(dip, +1);
433 gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
434
435 gfs2_trans_end(sdp);
436 if (dip->i_alloc.al_rgd)
437 gfs2_inplace_release(dip);
438 gfs2_quota_unlock(dip);
439 gfs2_alloc_put(dip);
440
441 gfs2_glock_dq_uninit_m(2, ghs);
442
David Teiglandb3b94fa2006-01-16 16:50:04 +0000443 d_instantiate(dentry, inode);
444 mark_inode_dirty(inode);
445
446 return 0;
447}
448
449/**
450 * gfs2_rmdir - Remove a directory
451 * @dir: The parent directory of the directory to be removed
452 * @dentry: The dentry of the directory to remove
453 *
454 * Remove a directory. Call gfs2_rmdiri()
455 *
456 * Returns: errno
457 */
458
459static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
460{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400461 struct gfs2_inode *dip = GFS2_I(dir);
462 struct gfs2_sbd *sdp = GFS2_SB(dir);
463 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
Russell Cattelanddee7602007-01-29 17:13:44 -0600464 struct gfs2_holder ghs[3];
465 struct gfs2_rgrpd *rgd;
466 struct gfs2_holder ri_gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000467 int error;
468
Russell Cattelanddee7602007-01-29 17:13:44 -0600469
470 error = gfs2_rindex_hold(sdp, &ri_gh);
471 if (error)
472 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000473 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
474 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
475
Russell Cattelanddee7602007-01-29 17:13:44 -0600476 rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr);
477 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
478
479 error = gfs2_glock_nq_m(3, ghs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000480 if (error)
481 goto out;
482
483 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
484 if (error)
485 goto out_gunlock;
486
487 if (ip->i_di.di_entries < 2) {
488 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500489 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000490 error = -EIO;
491 goto out_gunlock;
492 }
493 if (ip->i_di.di_entries > 2) {
494 error = -ENOTEMPTY;
495 goto out_gunlock;
496 }
497
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400498 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000499 if (error)
500 goto out_gunlock;
501
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400502 error = gfs2_rmdiri(dip, &dentry->d_name, ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000503
504 gfs2_trans_end(sdp);
505
Steven Whitehousea91ea692006-09-04 12:04:26 -0400506out_gunlock:
Russell Cattelanddee7602007-01-29 17:13:44 -0600507 gfs2_glock_dq_m(3, ghs);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400508out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000509 gfs2_holder_uninit(ghs);
510 gfs2_holder_uninit(ghs + 1);
Russell Cattelanddee7602007-01-29 17:13:44 -0600511 gfs2_holder_uninit(ghs + 2);
512 gfs2_glock_dq_uninit(&ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000513 return error;
514}
515
516/**
517 * gfs2_mknod - Make a special file
518 * @dir: The directory in which the special file will reside
519 * @dentry: The dentry of the special file
520 * @mode: The mode of the special file
521 * @rdev: The device specification of the special file
522 *
523 */
524
525static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
526 dev_t dev)
527{
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500528 struct gfs2_inode *dip = GFS2_I(dir);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400529 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000530 struct gfs2_holder ghs[2];
531 struct inode *inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000532
533 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
534
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500535 inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000536 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000537 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000538 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000539 }
540
David Teiglandb3b94fa2006-01-16 16:50:04 +0000541 gfs2_trans_end(sdp);
542 if (dip->i_alloc.al_rgd)
543 gfs2_inplace_release(dip);
544 gfs2_quota_unlock(dip);
545 gfs2_alloc_put(dip);
546
547 gfs2_glock_dq_uninit_m(2, ghs);
548
David Teiglandb3b94fa2006-01-16 16:50:04 +0000549 d_instantiate(dentry, inode);
550 mark_inode_dirty(inode);
551
552 return 0;
553}
554
555/**
556 * gfs2_rename - Rename a file
557 * @odir: Parent directory of old file name
558 * @odentry: The old dentry of the file
559 * @ndir: Parent directory of new file name
560 * @ndentry: The new dentry of the file
561 *
562 * Returns: errno
563 */
564
565static int gfs2_rename(struct inode *odir, struct dentry *odentry,
566 struct inode *ndir, struct dentry *ndentry)
567{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400568 struct gfs2_inode *odip = GFS2_I(odir);
569 struct gfs2_inode *ndip = GFS2_I(ndir);
570 struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000571 struct gfs2_inode *nip = NULL;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400572 struct gfs2_sbd *sdp = GFS2_SB(odir);
Russell Cattelanddee7602007-01-29 17:13:44 -0600573 struct gfs2_holder ghs[5], r_gh;
574 struct gfs2_rgrpd *nrgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000575 unsigned int num_gh;
576 int dir_rename = 0;
577 int alloc_required;
578 unsigned int x;
579 int error;
580
David Teiglandb3b94fa2006-01-16 16:50:04 +0000581 if (ndentry->d_inode) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400582 nip = GFS2_I(ndentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000583 if (ip == nip)
584 return 0;
585 }
586
David Teiglandb3b94fa2006-01-16 16:50:04 +0000587 /* Make sure we aren't trying to move a dirctory into it's subdir */
588
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500589 if (S_ISDIR(ip->i_inode.i_mode) && odip != ndip) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000590 dir_rename = 1;
591
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500592 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE, 0,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000593 &r_gh);
594 if (error)
595 goto out;
596
597 error = gfs2_ok_to_move(ip, ndip);
598 if (error)
599 goto out_gunlock_r;
600 }
601
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400602 num_gh = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000603 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400604 if (odip != ndip) {
605 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
606 num_gh++;
607 }
608 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
609 num_gh++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000610
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400611 if (nip) {
612 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
613 num_gh++;
Russell Cattelanddee7602007-01-29 17:13:44 -0600614 /* grab the resource lock for unlink flag twiddling
615 * this is the case of the target file already existing
616 * so we unlink before doing the rename
617 */
618 nrgd = gfs2_blk2rgrpd(sdp, nip->i_num.no_addr);
619 if (nrgd)
620 gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400621 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000622
623 error = gfs2_glock_nq_m(num_gh, ghs);
624 if (error)
625 goto out_uninit;
626
627 /* Check out the old directory */
628
629 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
630 if (error)
631 goto out_gunlock;
632
633 /* Check out the new directory */
634
635 if (nip) {
636 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
637 if (error)
638 goto out_gunlock;
639
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500640 if (S_ISDIR(nip->i_inode.i_mode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000641 if (nip->i_di.di_entries < 2) {
642 if (gfs2_consist_inode(nip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500643 gfs2_dinode_print(nip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000644 error = -EIO;
645 goto out_gunlock;
646 }
647 if (nip->i_di.di_entries > 2) {
648 error = -ENOTEMPTY;
649 goto out_gunlock;
650 }
651 }
652 } else {
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400653 error = permission(ndir, MAY_WRITE | MAY_EXEC, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000654 if (error)
655 goto out_gunlock;
656
Steven Whitehousec7526662006-03-20 12:30:04 -0500657 error = gfs2_dir_search(ndir, &ndentry->d_name, NULL, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000658 switch (error) {
659 case -ENOENT:
660 error = 0;
661 break;
662 case 0:
663 error = -EEXIST;
664 default:
665 goto out_gunlock;
666 };
667
668 if (odip != ndip) {
Steven Whitehouse4f561102006-11-01 14:04:17 -0500669 if (!ndip->i_inode.i_nlink) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000670 error = -EINVAL;
671 goto out_gunlock;
672 }
Steven Whitehousecd915492006-09-04 12:49:07 -0400673 if (ndip->i_di.di_entries == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000674 error = -EFBIG;
675 goto out_gunlock;
676 }
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500677 if (S_ISDIR(ip->i_inode.i_mode) &&
Steven Whitehouse4f561102006-11-01 14:04:17 -0500678 ndip->i_inode.i_nlink == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000679 error = -EMLINK;
680 goto out_gunlock;
681 }
682 }
683 }
684
685 /* Check out the dir to be renamed */
686
687 if (dir_rename) {
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400688 error = permission(odentry->d_inode, MAY_WRITE, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000689 if (error)
690 goto out_gunlock;
691 }
692
Steven Whitehousec7526662006-03-20 12:30:04 -0500693 alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
694 if (error < 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000695 goto out_gunlock;
Steven Whitehousec7526662006-03-20 12:30:04 -0500696 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000697
698 if (alloc_required) {
699 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
700
701 error = gfs2_quota_lock(ndip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
702 if (error)
703 goto out_alloc;
704
Steven Whitehouse2933f922006-11-01 13:23:29 -0500705 error = gfs2_quota_check(ndip, ndip->i_inode.i_uid, ndip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000706 if (error)
707 goto out_gunlock_q;
708
709 al->al_requested = sdp->sd_max_dirres;
710
711 error = gfs2_inplace_reserve(ndip);
712 if (error)
713 goto out_gunlock_q;
714
Steven Whitehousefe1bded2006-04-18 10:09:15 -0400715 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000716 al->al_rgd->rd_ri.ri_length +
717 4 * RES_DINODE + 4 * RES_LEAF +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500718 RES_STATFS + RES_QUOTA + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000719 if (error)
720 goto out_ipreserv;
721 } else {
722 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500723 5 * RES_LEAF + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000724 if (error)
725 goto out_gunlock;
726 }
727
728 /* Remove the target file, if it exists */
729
730 if (nip) {
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500731 if (S_ISDIR(nip->i_inode.i_mode))
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400732 error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
733 else {
734 error = gfs2_dir_del(ndip, &ndentry->d_name);
735 if (error)
736 goto out_end_trans;
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500737 error = gfs2_change_nlink(nip, -1);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400738 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000739 if (error)
740 goto out_end_trans;
741 }
742
743 if (dir_rename) {
744 struct qstr name;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500745 gfs2_str2qstr(&name, "..");
David Teiglandb3b94fa2006-01-16 16:50:04 +0000746
747 error = gfs2_change_nlink(ndip, +1);
748 if (error)
749 goto out_end_trans;
750 error = gfs2_change_nlink(odip, -1);
751 if (error)
752 goto out_end_trans;
753
754 error = gfs2_dir_mvino(ip, &name, &ndip->i_num, DT_DIR);
755 if (error)
756 goto out_end_trans;
757 } else {
758 struct buffer_head *dibh;
759 error = gfs2_meta_inode_buffer(ip, &dibh);
760 if (error)
761 goto out_end_trans;
Eric Sandeenddfe0622007-01-18 16:41:23 -0600762 ip->i_inode.i_ctime = CURRENT_TIME_SEC;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000763 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500764 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000765 brelse(dibh);
766 }
767
768 error = gfs2_dir_del(odip, &odentry->d_name);
769 if (error)
770 goto out_end_trans;
771
Steven Whitehousec7526662006-03-20 12:30:04 -0500772 error = gfs2_dir_add(ndir, &ndentry->d_name, &ip->i_num,
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500773 IF2DT(ip->i_inode.i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000774 if (error)
775 goto out_end_trans;
776
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400777out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000778 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400779out_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000780 if (alloc_required)
781 gfs2_inplace_release(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400782out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000783 if (alloc_required)
784 gfs2_quota_unlock(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400785out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000786 if (alloc_required)
787 gfs2_alloc_put(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400788out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000789 gfs2_glock_dq_m(num_gh, ghs);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400790out_uninit:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000791 for (x = 0; x < num_gh; x++)
792 gfs2_holder_uninit(ghs + x);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400793out_gunlock_r:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000794 if (dir_rename)
795 gfs2_glock_dq_uninit(&r_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400796out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000797 return error;
798}
799
800/**
801 * gfs2_readlink - Read the value of a symlink
802 * @dentry: the symlink
803 * @buf: the buffer to read the symlink data into
804 * @size: the size of the buffer
805 *
806 * Returns: errno
807 */
808
809static int gfs2_readlink(struct dentry *dentry, char __user *user_buf,
810 int user_size)
811{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400812 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000813 char array[GFS2_FAST_NAME_SIZE], *buf = array;
814 unsigned int len = GFS2_FAST_NAME_SIZE;
815 int error;
816
David Teiglandb3b94fa2006-01-16 16:50:04 +0000817 error = gfs2_readlinki(ip, &buf, &len);
818 if (error)
819 return error;
820
821 if (user_size > len - 1)
822 user_size = len - 1;
823
824 if (copy_to_user(user_buf, buf, user_size))
825 error = -EFAULT;
826 else
827 error = user_size;
828
829 if (buf != array)
830 kfree(buf);
831
832 return error;
833}
834
835/**
836 * gfs2_follow_link - Follow a symbolic link
837 * @dentry: The dentry of the link
838 * @nd: Data that we pass to vfs_follow_link()
839 *
840 * This can handle symlinks of any size. It is optimised for symlinks
841 * under GFS2_FAST_NAME_SIZE.
842 *
843 * Returns: 0 on success or error code
844 */
845
846static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
847{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400848 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000849 char array[GFS2_FAST_NAME_SIZE], *buf = array;
850 unsigned int len = GFS2_FAST_NAME_SIZE;
851 int error;
852
David Teiglandb3b94fa2006-01-16 16:50:04 +0000853 error = gfs2_readlinki(ip, &buf, &len);
854 if (!error) {
855 error = vfs_follow_link(nd, buf);
856 if (buf != array)
857 kfree(buf);
858 }
859
860 return ERR_PTR(error);
861}
862
863/**
864 * gfs2_permission -
865 * @inode:
866 * @mask:
867 * @nd: passed from Linux VFS, ignored by us
868 *
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500869 * This may be called from the VFS directly, or from within GFS2 with the
870 * inode locked, so we look to see if the glock is already locked and only
871 * lock the glock if its not already been done.
872 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000873 * Returns: errno
874 */
875
876static int gfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
877{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400878 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000879 struct gfs2_holder i_gh;
880 int error;
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500881 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000882
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500883 if (gfs2_glock_is_locked_by_me(ip->i_gl) == 0) {
884 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
885 if (error)
886 return error;
887 unlock = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000888 }
889
Steven Whitehouse77386e12006-11-29 10:41:49 -0500890 error = generic_permission(inode, mask, gfs2_check_acl);
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500891 if (unlock)
892 gfs2_glock_dq_uninit(&i_gh);
893
David Teiglandb3b94fa2006-01-16 16:50:04 +0000894 return error;
895}
896
897static int setattr_size(struct inode *inode, struct iattr *attr)
898{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400899 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000900 int error;
901
902 if (attr->ia_size != ip->i_di.di_size) {
903 error = vmtruncate(inode, attr->ia_size);
904 if (error)
905 return error;
906 }
907
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +0000908 error = gfs2_truncatei(ip, attr->ia_size);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000909 if (error)
910 return error;
911
912 return error;
913}
914
915static int setattr_chown(struct inode *inode, struct iattr *attr)
916{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400917 struct gfs2_inode *ip = GFS2_I(inode);
918 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000919 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400920 u32 ouid, ogid, nuid, ngid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000921 int error;
922
Steven Whitehouse2933f922006-11-01 13:23:29 -0500923 ouid = inode->i_uid;
924 ogid = inode->i_gid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000925 nuid = attr->ia_uid;
926 ngid = attr->ia_gid;
927
928 if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
929 ouid = nuid = NO_QUOTA_CHANGE;
930 if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
931 ogid = ngid = NO_QUOTA_CHANGE;
932
933 gfs2_alloc_get(ip);
934
935 error = gfs2_quota_lock(ip, nuid, ngid);
936 if (error)
937 goto out_alloc;
938
939 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
940 error = gfs2_quota_check(ip, nuid, ngid);
941 if (error)
942 goto out_gunlock_q;
943 }
944
945 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
946 if (error)
947 goto out_gunlock_q;
948
949 error = gfs2_meta_inode_buffer(ip, &dibh);
950 if (error)
951 goto out_end_trans;
952
953 error = inode_setattr(inode, attr);
954 gfs2_assert_warn(sdp, !error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000955
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000956 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500957 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000958 brelse(dibh);
959
960 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
Steven Whitehouseaf18ddb82006-06-24 15:42:21 -0400961 gfs2_quota_change(ip, -ip->i_di.di_blocks, ouid, ogid);
962 gfs2_quota_change(ip, ip->i_di.di_blocks, nuid, ngid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000963 }
964
Steven Whitehousea91ea692006-09-04 12:04:26 -0400965out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000966 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400967out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000968 gfs2_quota_unlock(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400969out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000970 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000971 return error;
972}
973
974/**
975 * gfs2_setattr - Change attributes on an inode
976 * @dentry: The dentry which is changing
977 * @attr: The structure describing the change
978 *
979 * The VFS layer wants to change one or more of an inodes attributes. Write
980 * that change out to disk.
981 *
982 * Returns: errno
983 */
984
985static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
986{
987 struct inode *inode = dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400988 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000989 struct gfs2_holder i_gh;
990 int error;
991
David Teiglandb3b94fa2006-01-16 16:50:04 +0000992 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
993 if (error)
994 return error;
995
996 error = -EPERM;
997 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
998 goto out;
999
1000 error = inode_change_ok(inode, attr);
1001 if (error)
1002 goto out;
1003
1004 if (attr->ia_valid & ATTR_SIZE)
1005 error = setattr_size(inode, attr);
1006 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1007 error = setattr_chown(inode, attr);
1008 else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1009 error = gfs2_acl_chmod(ip, attr);
1010 else
1011 error = gfs2_setattr_simple(ip, attr);
1012
Steven Whitehousea91ea692006-09-04 12:04:26 -04001013out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001014 gfs2_glock_dq_uninit(&i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001015 if (!error)
1016 mark_inode_dirty(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001017 return error;
1018}
1019
1020/**
1021 * gfs2_getattr - Read out an inode's attributes
Steven Whitehouse26c1a572006-09-04 15:32:10 -04001022 * @mnt: The vfsmount the inode is being accessed from
David Teiglandb3b94fa2006-01-16 16:50:04 +00001023 * @dentry: The dentry to stat
1024 * @stat: The inode's stats
1025 *
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001026 * This may be called from the VFS directly, or from within GFS2 with the
1027 * inode locked, so we look to see if the glock is already locked and only
1028 * lock the glock if its not already been done. Note that its the NFS
1029 * readdirplus operation which causes this to be called (from filldir)
1030 * with the glock already held.
1031 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001032 * Returns: errno
1033 */
1034
1035static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1036 struct kstat *stat)
1037{
1038 struct inode *inode = dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001039 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001040 struct gfs2_holder gh;
1041 int error;
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001042 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001043
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001044 if (gfs2_glock_is_locked_by_me(ip->i_gl) == 0) {
1045 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1046 if (error)
1047 return error;
1048 unlock = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001049 }
1050
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001051 generic_fillattr(inode, stat);
Steven Whitehoused7c103d2007-01-25 17:14:59 +00001052 if (unlock)
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001053 gfs2_glock_dq_uninit(&gh);
1054
1055 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001056}
1057
1058static int gfs2_setxattr(struct dentry *dentry, const char *name,
1059 const void *data, size_t size, int flags)
1060{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001061 struct inode *inode = dentry->d_inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001062 struct gfs2_ea_request er;
1063
David Teiglandb3b94fa2006-01-16 16:50:04 +00001064 memset(&er, 0, sizeof(struct gfs2_ea_request));
1065 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1066 if (er.er_type == GFS2_EATYPE_UNUSED)
1067 return -EOPNOTSUPP;
1068 er.er_data = (char *)data;
1069 er.er_name_len = strlen(er.er_name);
1070 er.er_data_len = size;
1071 er.er_flags = flags;
1072
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001073 gfs2_assert_warn(GFS2_SB(inode), !(er.er_flags & GFS2_ERF_MODE));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001074
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001075 return gfs2_ea_set(GFS2_I(inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001076}
1077
1078static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1079 void *data, size_t size)
1080{
1081 struct gfs2_ea_request er;
1082
David Teiglandb3b94fa2006-01-16 16:50:04 +00001083 memset(&er, 0, sizeof(struct gfs2_ea_request));
1084 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1085 if (er.er_type == GFS2_EATYPE_UNUSED)
1086 return -EOPNOTSUPP;
1087 er.er_data = data;
1088 er.er_name_len = strlen(er.er_name);
1089 er.er_data_len = size;
1090
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001091 return gfs2_ea_get(GFS2_I(dentry->d_inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001092}
1093
1094static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
1095{
1096 struct gfs2_ea_request er;
1097
David Teiglandb3b94fa2006-01-16 16:50:04 +00001098 memset(&er, 0, sizeof(struct gfs2_ea_request));
1099 er.er_data = (size) ? buffer : NULL;
1100 er.er_data_len = size;
1101
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001102 return gfs2_ea_list(GFS2_I(dentry->d_inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001103}
1104
1105static int gfs2_removexattr(struct dentry *dentry, const char *name)
1106{
1107 struct gfs2_ea_request er;
1108
David Teiglandb3b94fa2006-01-16 16:50:04 +00001109 memset(&er, 0, sizeof(struct gfs2_ea_request));
1110 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1111 if (er.er_type == GFS2_EATYPE_UNUSED)
1112 return -EOPNOTSUPP;
1113 er.er_name_len = strlen(er.er_name);
1114
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001115 return gfs2_ea_remove(GFS2_I(dentry->d_inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001116}
1117
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001118const struct inode_operations gfs2_file_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001119 .permission = gfs2_permission,
1120 .setattr = gfs2_setattr,
1121 .getattr = gfs2_getattr,
1122 .setxattr = gfs2_setxattr,
1123 .getxattr = gfs2_getxattr,
1124 .listxattr = gfs2_listxattr,
1125 .removexattr = gfs2_removexattr,
1126};
1127
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001128const struct inode_operations gfs2_dev_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001129 .permission = gfs2_permission,
1130 .setattr = gfs2_setattr,
1131 .getattr = gfs2_getattr,
1132 .setxattr = gfs2_setxattr,
1133 .getxattr = gfs2_getxattr,
1134 .listxattr = gfs2_listxattr,
1135 .removexattr = gfs2_removexattr,
1136};
1137
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001138const struct inode_operations gfs2_dir_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001139 .create = gfs2_create,
1140 .lookup = gfs2_lookup,
1141 .link = gfs2_link,
1142 .unlink = gfs2_unlink,
1143 .symlink = gfs2_symlink,
1144 .mkdir = gfs2_mkdir,
1145 .rmdir = gfs2_rmdir,
1146 .mknod = gfs2_mknod,
1147 .rename = gfs2_rename,
1148 .permission = gfs2_permission,
1149 .setattr = gfs2_setattr,
1150 .getattr = gfs2_getattr,
1151 .setxattr = gfs2_setxattr,
1152 .getxattr = gfs2_getxattr,
1153 .listxattr = gfs2_listxattr,
1154 .removexattr = gfs2_removexattr,
1155};
1156
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001157const struct inode_operations gfs2_symlink_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001158 .readlink = gfs2_readlink,
1159 .follow_link = gfs2_follow_link,
1160 .permission = gfs2_permission,
1161 .setattr = gfs2_setattr,
1162 .getattr = gfs2_getattr,
1163 .setxattr = gfs2_setxattr,
1164 .getxattr = gfs2_getxattr,
1165 .listxattr = gfs2_listxattr,
1166 .removexattr = gfs2_removexattr,
1167};
1168