blob: c10b914bf8cc40f979eab4961f9e54460f8cf93f [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
David Teiglandb3b94fa2006-01-16 16:50:04 +0000147 if (S_ISDIR(ip->i_di.di_mode))
148 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;
172 if (!dip->i_di.di_nlink)
173 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;
181 if (!ip->i_di.di_nlink)
182 goto out_gunlock;
183 error = -EMLINK;
Steven Whitehousecd915492006-09-04 12:49:07 -0400184 if (ip->i_di.di_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
199 error = gfs2_quota_check(dip, dip->i_di.di_uid,
200 dip->i_di.di_gid);
201 if (error)
202 goto out_gunlock_q;
203
204 al->al_requested = sdp->sd_max_dirres;
205
206 error = gfs2_inplace_reserve(dip);
207 if (error)
208 goto out_gunlock_q;
209
Steven Whitehouse1b502592006-05-18 14:10:52 -0400210 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000211 al->al_rgd->rd_ri.ri_length +
212 2 * RES_DINODE + RES_STATFS +
213 RES_QUOTA, 0);
214 if (error)
215 goto out_ipres;
216 } else {
217 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
218 if (error)
219 goto out_ipres;
220 }
221
Steven Whitehousec7526662006-03-20 12:30:04 -0500222 error = gfs2_dir_add(dir, &dentry->d_name, &ip->i_num,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000223 IF2DT(ip->i_di.di_mode));
224 if (error)
225 goto out_end_trans;
226
227 error = gfs2_change_nlink(ip, +1);
228
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400229out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000230 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400231out_ipres:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000232 if (alloc_required)
233 gfs2_inplace_release(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400234out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000235 if (alloc_required)
236 gfs2_quota_unlock(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400237out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000238 if (alloc_required)
239 gfs2_alloc_put(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400240out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000241 gfs2_glock_dq_m(2, ghs);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400242out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000243 gfs2_holder_uninit(ghs);
244 gfs2_holder_uninit(ghs + 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000245 if (!error) {
Steven Whitehouse29937ac2006-07-06 17:58:03 -0400246 atomic_inc(&inode->i_count);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000247 d_instantiate(dentry, inode);
248 mark_inode_dirty(inode);
249 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000250 return error;
251}
252
253/**
254 * gfs2_unlink - Unlink a file
255 * @dir: The inode of the directory containing the file to unlink
256 * @dentry: The file itself
257 *
258 * Unlink a file. Call gfs2_unlinki()
259 *
260 * Returns: errno
261 */
262
263static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
264{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400265 struct gfs2_inode *dip = GFS2_I(dir);
266 struct gfs2_sbd *sdp = GFS2_SB(dir);
267 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000268 struct gfs2_holder ghs[2];
269 int error;
270
David Teiglandb3b94fa2006-01-16 16:50:04 +0000271 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
272 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
273
274 error = gfs2_glock_nq_m(2, ghs);
275 if (error)
276 goto out;
277
278 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
279 if (error)
280 goto out_gunlock;
281
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400282 error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000283 if (error)
284 goto out_gunlock;
285
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400286 error = gfs2_dir_del(dip, &dentry->d_name);
287 if (error)
288 goto out_end_trans;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000289
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400290 error = gfs2_change_nlink(ip, -1);
291
292out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000293 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400294out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000295 gfs2_glock_dq_m(2, ghs);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400296out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000297 gfs2_holder_uninit(ghs);
298 gfs2_holder_uninit(ghs + 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000299 return error;
300}
301
302/**
303 * gfs2_symlink - Create a symlink
304 * @dir: The directory to create the symlink in
305 * @dentry: The dentry to put the symlink in
306 * @symname: The thing which the link points to
307 *
308 * Returns: errno
309 */
310
311static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
312 const char *symname)
313{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400314 struct gfs2_inode *dip = GFS2_I(dir), *ip;
315 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000316 struct gfs2_holder ghs[2];
317 struct inode *inode;
318 struct buffer_head *dibh;
319 int size;
320 int error;
321
David Teiglandb3b94fa2006-01-16 16:50:04 +0000322 /* Must be stuffed with a null terminator for gfs2_follow_link() */
323 size = strlen(symname);
324 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
325 return -ENAMETOOLONG;
326
327 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
328
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500329 inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000330 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000331 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000332 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000333 }
334
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500335 ip = ghs[1].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000336
337 ip->i_di.di_size = size;
338
339 error = gfs2_meta_inode_buffer(ip, &dibh);
340
341 if (!gfs2_assert_withdraw(sdp, !error)) {
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500342 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000343 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
344 size);
345 brelse(dibh);
346 }
347
348 gfs2_trans_end(sdp);
349 if (dip->i_alloc.al_rgd)
350 gfs2_inplace_release(dip);
351 gfs2_quota_unlock(dip);
352 gfs2_alloc_put(dip);
353
354 gfs2_glock_dq_uninit_m(2, ghs);
355
David Teiglandb3b94fa2006-01-16 16:50:04 +0000356 d_instantiate(dentry, inode);
357 mark_inode_dirty(inode);
358
359 return 0;
360}
361
362/**
363 * gfs2_mkdir - Make a directory
364 * @dir: The parent directory of the new one
365 * @dentry: The dentry of the new directory
366 * @mode: The mode of the new directory
367 *
368 * Returns: errno
369 */
370
371static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
372{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400373 struct gfs2_inode *dip = GFS2_I(dir), *ip;
374 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000375 struct gfs2_holder ghs[2];
376 struct inode *inode;
377 struct buffer_head *dibh;
378 int error;
379
David Teiglandb3b94fa2006-01-16 16:50:04 +0000380 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
381
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500382 inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000383 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000384 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000385 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000386 }
387
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500388 ip = ghs[1].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000389
390 ip->i_di.di_nlink = 2;
391 ip->i_di.di_size = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
392 ip->i_di.di_flags |= GFS2_DIF_JDATA;
393 ip->i_di.di_payload_format = GFS2_FORMAT_DE;
394 ip->i_di.di_entries = 2;
395
396 error = gfs2_meta_inode_buffer(ip, &dibh);
397
398 if (!gfs2_assert_withdraw(sdp, !error)) {
399 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
Steven Whitehousec7526662006-03-20 12:30:04 -0500400 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500401 struct qstr str;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000402
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500403 gfs2_str2qstr(&str, ".");
Steven Whitehousec7526662006-03-20 12:30:04 -0500404 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
405 gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000406 dent->de_inum = di->di_num; /* already GFS2 endian */
Steven Whitehouse7ecdb702006-10-03 21:03:35 -0400407 dent->de_type = cpu_to_be16(DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000408 di->di_entries = cpu_to_be32(1);
409
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500410 gfs2_str2qstr(&str, "..");
Steven Whitehousec7526662006-03-20 12:30:04 -0500411 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
412 gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000413
Steven Whitehouse409e1852006-10-02 14:20:43 -0400414 gfs2_inum_out(&dip->i_num, &dent->de_inum);
Steven Whitehouse7ecdb702006-10-03 21:03:35 -0400415 dent->de_type = cpu_to_be16(DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000416
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500417 gfs2_dinode_out(ip, di);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000418
419 brelse(dibh);
420 }
421
422 error = gfs2_change_nlink(dip, +1);
423 gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
424
425 gfs2_trans_end(sdp);
426 if (dip->i_alloc.al_rgd)
427 gfs2_inplace_release(dip);
428 gfs2_quota_unlock(dip);
429 gfs2_alloc_put(dip);
430
431 gfs2_glock_dq_uninit_m(2, ghs);
432
David Teiglandb3b94fa2006-01-16 16:50:04 +0000433 d_instantiate(dentry, inode);
434 mark_inode_dirty(inode);
435
436 return 0;
437}
438
439/**
440 * gfs2_rmdir - Remove a directory
441 * @dir: The parent directory of the directory to be removed
442 * @dentry: The dentry of the directory to remove
443 *
444 * Remove a directory. Call gfs2_rmdiri()
445 *
446 * Returns: errno
447 */
448
449static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
450{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400451 struct gfs2_inode *dip = GFS2_I(dir);
452 struct gfs2_sbd *sdp = GFS2_SB(dir);
453 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000454 struct gfs2_holder ghs[2];
455 int error;
456
David Teiglandb3b94fa2006-01-16 16:50:04 +0000457 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
458 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
459
460 error = gfs2_glock_nq_m(2, ghs);
461 if (error)
462 goto out;
463
464 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
465 if (error)
466 goto out_gunlock;
467
468 if (ip->i_di.di_entries < 2) {
469 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500470 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000471 error = -EIO;
472 goto out_gunlock;
473 }
474 if (ip->i_di.di_entries > 2) {
475 error = -ENOTEMPTY;
476 goto out_gunlock;
477 }
478
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400479 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000480 if (error)
481 goto out_gunlock;
482
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400483 error = gfs2_rmdiri(dip, &dentry->d_name, ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000484
485 gfs2_trans_end(sdp);
486
Steven Whitehousea91ea692006-09-04 12:04:26 -0400487out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000488 gfs2_glock_dq_m(2, ghs);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400489out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000490 gfs2_holder_uninit(ghs);
491 gfs2_holder_uninit(ghs + 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000492 return error;
493}
494
495/**
496 * gfs2_mknod - Make a special file
497 * @dir: The directory in which the special file will reside
498 * @dentry: The dentry of the special file
499 * @mode: The mode of the special file
500 * @rdev: The device specification of the special file
501 *
502 */
503
504static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
505 dev_t dev)
506{
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500507 struct gfs2_inode *dip = GFS2_I(dir);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400508 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000509 struct gfs2_holder ghs[2];
510 struct inode *inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000511
512 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
513
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500514 inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000515 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000516 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000517 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000518 }
519
David Teiglandb3b94fa2006-01-16 16:50:04 +0000520 gfs2_trans_end(sdp);
521 if (dip->i_alloc.al_rgd)
522 gfs2_inplace_release(dip);
523 gfs2_quota_unlock(dip);
524 gfs2_alloc_put(dip);
525
526 gfs2_glock_dq_uninit_m(2, ghs);
527
David Teiglandb3b94fa2006-01-16 16:50:04 +0000528 d_instantiate(dentry, inode);
529 mark_inode_dirty(inode);
530
531 return 0;
532}
533
534/**
535 * gfs2_rename - Rename a file
536 * @odir: Parent directory of old file name
537 * @odentry: The old dentry of the file
538 * @ndir: Parent directory of new file name
539 * @ndentry: The new dentry of the file
540 *
541 * Returns: errno
542 */
543
544static int gfs2_rename(struct inode *odir, struct dentry *odentry,
545 struct inode *ndir, struct dentry *ndentry)
546{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400547 struct gfs2_inode *odip = GFS2_I(odir);
548 struct gfs2_inode *ndip = GFS2_I(ndir);
549 struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000550 struct gfs2_inode *nip = NULL;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400551 struct gfs2_sbd *sdp = GFS2_SB(odir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000552 struct gfs2_holder ghs[4], r_gh;
553 unsigned int num_gh;
554 int dir_rename = 0;
555 int alloc_required;
556 unsigned int x;
557 int error;
558
David Teiglandb3b94fa2006-01-16 16:50:04 +0000559 if (ndentry->d_inode) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400560 nip = GFS2_I(ndentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000561 if (ip == nip)
562 return 0;
563 }
564
David Teiglandb3b94fa2006-01-16 16:50:04 +0000565 /* Make sure we aren't trying to move a dirctory into it's subdir */
566
567 if (S_ISDIR(ip->i_di.di_mode) && odip != ndip) {
568 dir_rename = 1;
569
570 error = gfs2_glock_nq_init(sdp->sd_rename_gl,
571 LM_ST_EXCLUSIVE, 0,
572 &r_gh);
573 if (error)
574 goto out;
575
576 error = gfs2_ok_to_move(ip, ndip);
577 if (error)
578 goto out_gunlock_r;
579 }
580
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400581 num_gh = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000582 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400583 if (odip != ndip) {
584 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
585 num_gh++;
586 }
587 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
588 num_gh++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000589
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400590 if (nip) {
591 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
592 num_gh++;
593 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000594
595 error = gfs2_glock_nq_m(num_gh, ghs);
596 if (error)
597 goto out_uninit;
598
599 /* Check out the old directory */
600
601 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
602 if (error)
603 goto out_gunlock;
604
605 /* Check out the new directory */
606
607 if (nip) {
608 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
609 if (error)
610 goto out_gunlock;
611
612 if (S_ISDIR(nip->i_di.di_mode)) {
613 if (nip->i_di.di_entries < 2) {
614 if (gfs2_consist_inode(nip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500615 gfs2_dinode_print(nip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000616 error = -EIO;
617 goto out_gunlock;
618 }
619 if (nip->i_di.di_entries > 2) {
620 error = -ENOTEMPTY;
621 goto out_gunlock;
622 }
623 }
624 } else {
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400625 error = permission(ndir, MAY_WRITE | MAY_EXEC, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000626 if (error)
627 goto out_gunlock;
628
Steven Whitehousec7526662006-03-20 12:30:04 -0500629 error = gfs2_dir_search(ndir, &ndentry->d_name, NULL, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000630 switch (error) {
631 case -ENOENT:
632 error = 0;
633 break;
634 case 0:
635 error = -EEXIST;
636 default:
637 goto out_gunlock;
638 };
639
640 if (odip != ndip) {
641 if (!ndip->i_di.di_nlink) {
642 error = -EINVAL;
643 goto out_gunlock;
644 }
Steven Whitehousecd915492006-09-04 12:49:07 -0400645 if (ndip->i_di.di_entries == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000646 error = -EFBIG;
647 goto out_gunlock;
648 }
649 if (S_ISDIR(ip->i_di.di_mode) &&
Steven Whitehousecd915492006-09-04 12:49:07 -0400650 ndip->i_di.di_nlink == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000651 error = -EMLINK;
652 goto out_gunlock;
653 }
654 }
655 }
656
657 /* Check out the dir to be renamed */
658
659 if (dir_rename) {
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400660 error = permission(odentry->d_inode, MAY_WRITE, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000661 if (error)
662 goto out_gunlock;
663 }
664
Steven Whitehousec7526662006-03-20 12:30:04 -0500665 alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
666 if (error < 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000667 goto out_gunlock;
Steven Whitehousec7526662006-03-20 12:30:04 -0500668 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000669
670 if (alloc_required) {
671 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
672
673 error = gfs2_quota_lock(ndip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
674 if (error)
675 goto out_alloc;
676
677 error = gfs2_quota_check(ndip, ndip->i_di.di_uid,
678 ndip->i_di.di_gid);
679 if (error)
680 goto out_gunlock_q;
681
682 al->al_requested = sdp->sd_max_dirres;
683
684 error = gfs2_inplace_reserve(ndip);
685 if (error)
686 goto out_gunlock_q;
687
Steven Whitehousefe1bded2006-04-18 10:09:15 -0400688 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000689 al->al_rgd->rd_ri.ri_length +
690 4 * RES_DINODE + 4 * RES_LEAF +
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400691 RES_STATFS + RES_QUOTA, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000692 if (error)
693 goto out_ipreserv;
694 } else {
695 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400696 5 * RES_LEAF, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000697 if (error)
698 goto out_gunlock;
699 }
700
701 /* Remove the target file, if it exists */
702
703 if (nip) {
704 if (S_ISDIR(nip->i_di.di_mode))
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400705 error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
706 else {
707 error = gfs2_dir_del(ndip, &ndentry->d_name);
708 if (error)
709 goto out_end_trans;
710 error = gfs2_change_nlink(nip, -1);
711 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000712 if (error)
713 goto out_end_trans;
714 }
715
716 if (dir_rename) {
717 struct qstr name;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500718 gfs2_str2qstr(&name, "..");
David Teiglandb3b94fa2006-01-16 16:50:04 +0000719
720 error = gfs2_change_nlink(ndip, +1);
721 if (error)
722 goto out_end_trans;
723 error = gfs2_change_nlink(odip, -1);
724 if (error)
725 goto out_end_trans;
726
727 error = gfs2_dir_mvino(ip, &name, &ndip->i_num, DT_DIR);
728 if (error)
729 goto out_end_trans;
730 } else {
731 struct buffer_head *dibh;
732 error = gfs2_meta_inode_buffer(ip, &dibh);
733 if (error)
734 goto out_end_trans;
735 ip->i_di.di_ctime = get_seconds();
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000736 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500737 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000738 brelse(dibh);
739 }
740
741 error = gfs2_dir_del(odip, &odentry->d_name);
742 if (error)
743 goto out_end_trans;
744
Steven Whitehousec7526662006-03-20 12:30:04 -0500745 error = gfs2_dir_add(ndir, &ndentry->d_name, &ip->i_num,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000746 IF2DT(ip->i_di.di_mode));
747 if (error)
748 goto out_end_trans;
749
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400750out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000751 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400752out_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000753 if (alloc_required)
754 gfs2_inplace_release(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400755out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000756 if (alloc_required)
757 gfs2_quota_unlock(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400758out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000759 if (alloc_required)
760 gfs2_alloc_put(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400761out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000762 gfs2_glock_dq_m(num_gh, ghs);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400763out_uninit:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000764 for (x = 0; x < num_gh; x++)
765 gfs2_holder_uninit(ghs + x);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400766out_gunlock_r:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000767 if (dir_rename)
768 gfs2_glock_dq_uninit(&r_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400769out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000770 return error;
771}
772
773/**
774 * gfs2_readlink - Read the value of a symlink
775 * @dentry: the symlink
776 * @buf: the buffer to read the symlink data into
777 * @size: the size of the buffer
778 *
779 * Returns: errno
780 */
781
782static int gfs2_readlink(struct dentry *dentry, char __user *user_buf,
783 int user_size)
784{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400785 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000786 char array[GFS2_FAST_NAME_SIZE], *buf = array;
787 unsigned int len = GFS2_FAST_NAME_SIZE;
788 int error;
789
David Teiglandb3b94fa2006-01-16 16:50:04 +0000790 error = gfs2_readlinki(ip, &buf, &len);
791 if (error)
792 return error;
793
794 if (user_size > len - 1)
795 user_size = len - 1;
796
797 if (copy_to_user(user_buf, buf, user_size))
798 error = -EFAULT;
799 else
800 error = user_size;
801
802 if (buf != array)
803 kfree(buf);
804
805 return error;
806}
807
808/**
809 * gfs2_follow_link - Follow a symbolic link
810 * @dentry: The dentry of the link
811 * @nd: Data that we pass to vfs_follow_link()
812 *
813 * This can handle symlinks of any size. It is optimised for symlinks
814 * under GFS2_FAST_NAME_SIZE.
815 *
816 * Returns: 0 on success or error code
817 */
818
819static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
820{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400821 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000822 char array[GFS2_FAST_NAME_SIZE], *buf = array;
823 unsigned int len = GFS2_FAST_NAME_SIZE;
824 int error;
825
David Teiglandb3b94fa2006-01-16 16:50:04 +0000826 error = gfs2_readlinki(ip, &buf, &len);
827 if (!error) {
828 error = vfs_follow_link(nd, buf);
829 if (buf != array)
830 kfree(buf);
831 }
832
833 return ERR_PTR(error);
834}
835
836/**
837 * gfs2_permission -
838 * @inode:
839 * @mask:
840 * @nd: passed from Linux VFS, ignored by us
841 *
842 * Returns: errno
843 */
844
845static int gfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
846{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400847 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000848 struct gfs2_holder i_gh;
849 int error;
850
David Teiglandb3b94fa2006-01-16 16:50:04 +0000851 if (ip->i_vn == ip->i_gl->gl_vn)
852 return generic_permission(inode, mask, gfs2_check_acl);
853
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400854 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000855 if (!error) {
856 error = generic_permission(inode, mask, gfs2_check_acl_locked);
857 gfs2_glock_dq_uninit(&i_gh);
858 }
859
860 return error;
861}
862
863static int setattr_size(struct inode *inode, struct iattr *attr)
864{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400865 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000866 int error;
867
868 if (attr->ia_size != ip->i_di.di_size) {
869 error = vmtruncate(inode, attr->ia_size);
870 if (error)
871 return error;
872 }
873
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +0000874 error = gfs2_truncatei(ip, attr->ia_size);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000875 if (error)
876 return error;
877
878 return error;
879}
880
881static int setattr_chown(struct inode *inode, struct iattr *attr)
882{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400883 struct gfs2_inode *ip = GFS2_I(inode);
884 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000885 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400886 u32 ouid, ogid, nuid, ngid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000887 int error;
888
889 ouid = ip->i_di.di_uid;
890 ogid = ip->i_di.di_gid;
891 nuid = attr->ia_uid;
892 ngid = attr->ia_gid;
893
894 if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
895 ouid = nuid = NO_QUOTA_CHANGE;
896 if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
897 ogid = ngid = NO_QUOTA_CHANGE;
898
899 gfs2_alloc_get(ip);
900
901 error = gfs2_quota_lock(ip, nuid, ngid);
902 if (error)
903 goto out_alloc;
904
905 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
906 error = gfs2_quota_check(ip, nuid, ngid);
907 if (error)
908 goto out_gunlock_q;
909 }
910
911 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
912 if (error)
913 goto out_gunlock_q;
914
915 error = gfs2_meta_inode_buffer(ip, &dibh);
916 if (error)
917 goto out_end_trans;
918
919 error = inode_setattr(inode, attr);
920 gfs2_assert_warn(sdp, !error);
921 gfs2_inode_attr_out(ip);
922
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000923 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500924 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000925 brelse(dibh);
926
927 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
Steven Whitehouseaf18ddb82006-06-24 15:42:21 -0400928 gfs2_quota_change(ip, -ip->i_di.di_blocks, ouid, ogid);
929 gfs2_quota_change(ip, ip->i_di.di_blocks, nuid, ngid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000930 }
931
Steven Whitehousea91ea692006-09-04 12:04:26 -0400932out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000933 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400934out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000935 gfs2_quota_unlock(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400936out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000937 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000938 return error;
939}
940
941/**
942 * gfs2_setattr - Change attributes on an inode
943 * @dentry: The dentry which is changing
944 * @attr: The structure describing the change
945 *
946 * The VFS layer wants to change one or more of an inodes attributes. Write
947 * that change out to disk.
948 *
949 * Returns: errno
950 */
951
952static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
953{
954 struct inode *inode = dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400955 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000956 struct gfs2_holder i_gh;
957 int error;
958
David Teiglandb3b94fa2006-01-16 16:50:04 +0000959 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
960 if (error)
961 return error;
962
963 error = -EPERM;
964 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
965 goto out;
966
967 error = inode_change_ok(inode, attr);
968 if (error)
969 goto out;
970
971 if (attr->ia_valid & ATTR_SIZE)
972 error = setattr_size(inode, attr);
973 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
974 error = setattr_chown(inode, attr);
975 else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
976 error = gfs2_acl_chmod(ip, attr);
977 else
978 error = gfs2_setattr_simple(ip, attr);
979
Steven Whitehousea91ea692006-09-04 12:04:26 -0400980out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000981 gfs2_glock_dq_uninit(&i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000982 if (!error)
983 mark_inode_dirty(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000984 return error;
985}
986
987/**
988 * gfs2_getattr - Read out an inode's attributes
Steven Whitehouse26c1a572006-09-04 15:32:10 -0400989 * @mnt: The vfsmount the inode is being accessed from
David Teiglandb3b94fa2006-01-16 16:50:04 +0000990 * @dentry: The dentry to stat
991 * @stat: The inode's stats
992 *
993 * Returns: errno
994 */
995
996static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
997 struct kstat *stat)
998{
999 struct inode *inode = dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001000 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001001 struct gfs2_holder gh;
1002 int error;
1003
David Teiglandb3b94fa2006-01-16 16:50:04 +00001004 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1005 if (!error) {
1006 generic_fillattr(inode, stat);
1007 gfs2_glock_dq_uninit(&gh);
1008 }
1009
1010 return error;
1011}
1012
1013static int gfs2_setxattr(struct dentry *dentry, const char *name,
1014 const void *data, size_t size, int flags)
1015{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001016 struct inode *inode = dentry->d_inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001017 struct gfs2_ea_request er;
1018
David Teiglandb3b94fa2006-01-16 16:50:04 +00001019 memset(&er, 0, sizeof(struct gfs2_ea_request));
1020 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1021 if (er.er_type == GFS2_EATYPE_UNUSED)
1022 return -EOPNOTSUPP;
1023 er.er_data = (char *)data;
1024 er.er_name_len = strlen(er.er_name);
1025 er.er_data_len = size;
1026 er.er_flags = flags;
1027
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001028 gfs2_assert_warn(GFS2_SB(inode), !(er.er_flags & GFS2_ERF_MODE));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001029
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001030 return gfs2_ea_set(GFS2_I(inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001031}
1032
1033static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1034 void *data, size_t size)
1035{
1036 struct gfs2_ea_request er;
1037
David Teiglandb3b94fa2006-01-16 16:50:04 +00001038 memset(&er, 0, sizeof(struct gfs2_ea_request));
1039 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1040 if (er.er_type == GFS2_EATYPE_UNUSED)
1041 return -EOPNOTSUPP;
1042 er.er_data = data;
1043 er.er_name_len = strlen(er.er_name);
1044 er.er_data_len = size;
1045
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001046 return gfs2_ea_get(GFS2_I(dentry->d_inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001047}
1048
1049static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
1050{
1051 struct gfs2_ea_request er;
1052
David Teiglandb3b94fa2006-01-16 16:50:04 +00001053 memset(&er, 0, sizeof(struct gfs2_ea_request));
1054 er.er_data = (size) ? buffer : NULL;
1055 er.er_data_len = size;
1056
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001057 return gfs2_ea_list(GFS2_I(dentry->d_inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001058}
1059
1060static int gfs2_removexattr(struct dentry *dentry, const char *name)
1061{
1062 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_name_len = strlen(er.er_name);
1069
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001070 return gfs2_ea_remove(GFS2_I(dentry->d_inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001071}
1072
1073struct inode_operations gfs2_file_iops = {
1074 .permission = gfs2_permission,
1075 .setattr = gfs2_setattr,
1076 .getattr = gfs2_getattr,
1077 .setxattr = gfs2_setxattr,
1078 .getxattr = gfs2_getxattr,
1079 .listxattr = gfs2_listxattr,
1080 .removexattr = gfs2_removexattr,
1081};
1082
1083struct inode_operations gfs2_dev_iops = {
1084 .permission = gfs2_permission,
1085 .setattr = gfs2_setattr,
1086 .getattr = gfs2_getattr,
1087 .setxattr = gfs2_setxattr,
1088 .getxattr = gfs2_getxattr,
1089 .listxattr = gfs2_listxattr,
1090 .removexattr = gfs2_removexattr,
1091};
1092
1093struct inode_operations gfs2_dir_iops = {
1094 .create = gfs2_create,
1095 .lookup = gfs2_lookup,
1096 .link = gfs2_link,
1097 .unlink = gfs2_unlink,
1098 .symlink = gfs2_symlink,
1099 .mkdir = gfs2_mkdir,
1100 .rmdir = gfs2_rmdir,
1101 .mknod = gfs2_mknod,
1102 .rename = gfs2_rename,
1103 .permission = gfs2_permission,
1104 .setattr = gfs2_setattr,
1105 .getattr = gfs2_getattr,
1106 .setxattr = gfs2_setxattr,
1107 .getxattr = gfs2_getxattr,
1108 .listxattr = gfs2_listxattr,
1109 .removexattr = gfs2_removexattr,
1110};
1111
1112struct inode_operations gfs2_symlink_iops = {
1113 .readlink = gfs2_readlink,
1114 .follow_link = gfs2_follow_link,
1115 .permission = gfs2_permission,
1116 .setattr = gfs2_setattr,
1117 .getattr = gfs2_getattr,
1118 .setxattr = gfs2_setxattr,
1119 .getxattr = gfs2_getxattr,
1120 .listxattr = gfs2_listxattr,
1121 .removexattr = gfs2_removexattr,
1122};
1123