[PATCH] mark struct inode_operations const 2
[deliverable/linux.git] / fs / gfs2 / ops_inode.c
CommitLineData
b3b94faa
DT
1/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3a8a9a10 3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
b3b94faa
DT
4 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
e9fc2aa0 7 * of the GNU General Public License version 2.
b3b94faa
DT
8 */
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>
5c676f6d 20#include <linux/gfs2_ondisk.h>
71b86f56 21#include <linux/crc32.h>
7d308590 22#include <linux/lm_interface.h>
b3b94faa
DT
23#include <asm/uaccess.h>
24
25#include "gfs2.h"
5c676f6d 26#include "incore.h"
b3b94faa
DT
27#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"
b3b94faa
DT
37#include "quota.h"
38#include "rgrp.h"
39#include "trans.h"
5c676f6d 40#include "util.h"
b3b94faa
DT
41
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{
feaa7bba
SW
54 struct gfs2_inode *dip = GFS2_I(dir);
55 struct gfs2_sbd *sdp = GFS2_SB(dir);
b3b94faa
DT
56 struct gfs2_holder ghs[2];
57 struct inode *inode;
b3b94faa 58
b3b94faa
DT
59 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
60
61 for (;;) {
e7f14f4d 62 inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0);
7359a19c 63 if (!IS_ERR(inode)) {
b3b94faa
DT
64 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);
3a8476dd 70 mark_inode_dirty(inode);
b3b94faa 71 break;
7359a19c 72 } else if (PTR_ERR(inode) != -EEXIST ||
b3b94faa
DT
73 (nd->intent.open.flags & O_EXCL)) {
74 gfs2_holder_uninit(ghs);
7359a19c 75 return PTR_ERR(inode);
b3b94faa
DT
76 }
77
c752666c
SW
78 inode = gfs2_lookupi(dir, &dentry->d_name, 0, nd);
79 if (inode) {
80 if (!IS_ERR(inode)) {
c752666c
SW
81 gfs2_holder_uninit(ghs);
82 break;
83 } else {
84 gfs2_holder_uninit(ghs);
85 return PTR_ERR(inode);
86 }
b3b94faa
DT
87 }
88 }
89
b3b94faa 90 d_instantiate(dentry, inode);
b3b94faa
DT
91
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{
b3b94faa 109 struct inode *inode = NULL;
b3b94faa 110
c752666c 111 dentry->d_op = &gfs2_dops;
b3b94faa 112
c752666c
SW
113 inode = gfs2_lookupi(dir, &dentry->d_name, 0, nd);
114 if (inode && IS_ERR(inode))
115 return ERR_PTR(PTR_ERR(inode));
b3b94faa
DT
116
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{
feaa7bba
SW
139 struct gfs2_inode *dip = GFS2_I(dir);
140 struct gfs2_sbd *sdp = GFS2_SB(dir);
b3b94faa 141 struct inode *inode = old_dentry->d_inode;
feaa7bba 142 struct gfs2_inode *ip = GFS2_I(inode);
b3b94faa
DT
143 struct gfs2_holder ghs[2];
144 int alloc_required;
145 int error;
146
b60623c2 147 if (S_ISDIR(inode->i_mode))
b3b94faa
DT
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
faf450ef 157 error = permission(dir, MAY_WRITE | MAY_EXEC, NULL);
b3b94faa
DT
158 if (error)
159 goto out_gunlock;
160
c752666c 161 error = gfs2_dir_search(dir, &dentry->d_name, NULL, NULL);
b3b94faa
DT
162 switch (error) {
163 case -ENOENT:
164 break;
165 case 0:
166 error = -EEXIST;
167 default:
168 goto out_gunlock;
169 }
170
171 error = -EINVAL;
4f56110a 172 if (!dip->i_inode.i_nlink)
b3b94faa
DT
173 goto out_gunlock;
174 error = -EFBIG;
cd915493 175 if (dip->i_di.di_entries == (u32)-1)
b3b94faa
DT
176 goto out_gunlock;
177 error = -EPERM;
178 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
179 goto out_gunlock;
180 error = -EINVAL;
4f56110a 181 if (!ip->i_inode.i_nlink)
b3b94faa
DT
182 goto out_gunlock;
183 error = -EMLINK;
4f56110a 184 if (ip->i_inode.i_nlink == (u32)-1)
b3b94faa
DT
185 goto out_gunlock;
186
c752666c
SW
187 alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
188 if (error < 0)
b3b94faa 189 goto out_gunlock;
c752666c 190 error = 0;
b3b94faa
DT
191
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
2933f925 199 error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
b3b94faa
DT
200 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
1b50259b 209 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
b3b94faa
DT
210 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
c752666c 221 error = gfs2_dir_add(dir, &dentry->d_name, &ip->i_num,
b60623c2 222 IF2DT(inode->i_mode));
b3b94faa
DT
223 if (error)
224 goto out_end_trans;
225
226 error = gfs2_change_nlink(ip, +1);
227
feaa7bba 228out_end_trans:
b3b94faa 229 gfs2_trans_end(sdp);
feaa7bba 230out_ipres:
b3b94faa
DT
231 if (alloc_required)
232 gfs2_inplace_release(dip);
feaa7bba 233out_gunlock_q:
b3b94faa
DT
234 if (alloc_required)
235 gfs2_quota_unlock(dip);
feaa7bba 236out_alloc:
b3b94faa
DT
237 if (alloc_required)
238 gfs2_alloc_put(dip);
feaa7bba 239out_gunlock:
b3b94faa 240 gfs2_glock_dq_m(2, ghs);
feaa7bba 241out:
b3b94faa
DT
242 gfs2_holder_uninit(ghs);
243 gfs2_holder_uninit(ghs + 1);
b3b94faa 244 if (!error) {
29937ac6 245 atomic_inc(&inode->i_count);
b3b94faa
DT
246 d_instantiate(dentry, inode);
247 mark_inode_dirty(inode);
248 }
b3b94faa
DT
249 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{
feaa7bba
SW
264 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);
ddee7608
RC
267 struct gfs2_holder ghs[3];
268 struct gfs2_rgrpd *rgd;
269 struct gfs2_holder ri_gh;
b3b94faa
DT
270 int error;
271
ddee7608
RC
272 error = gfs2_rindex_hold(sdp, &ri_gh);
273 if (error)
274 return error;
275
b3b94faa 276 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
ddee7608 277 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
b3b94faa 278
ddee7608
RC
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);
b3b94faa
DT
284 if (error)
285 goto out;
286
287 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
288 if (error)
289 goto out_gunlock;
290
feaa7bba 291 error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
b3b94faa
DT
292 if (error)
293 goto out_gunlock;
294
feaa7bba
SW
295 error = gfs2_dir_del(dip, &dentry->d_name);
296 if (error)
297 goto out_end_trans;
b3b94faa 298
feaa7bba 299 error = gfs2_change_nlink(ip, -1);
b3b94faa 300
feaa7bba
SW
301out_end_trans:
302 gfs2_trans_end(sdp);
303out_gunlock:
ddee7608 304 gfs2_glock_dq_m(3, ghs);
feaa7bba 305out:
b3b94faa
DT
306 gfs2_holder_uninit(ghs);
307 gfs2_holder_uninit(ghs + 1);
ddee7608
RC
308 gfs2_holder_uninit(ghs + 2);
309 gfs2_glock_dq_uninit(&ri_gh);
b3b94faa
DT
310 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{
feaa7bba
SW
325 struct gfs2_inode *dip = GFS2_I(dir), *ip;
326 struct gfs2_sbd *sdp = GFS2_SB(dir);
b3b94faa
DT
327 struct gfs2_holder ghs[2];
328 struct inode *inode;
329 struct buffer_head *dibh;
330 int size;
331 int error;
332
b3b94faa
DT
333 /* 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
e7f14f4d 340 inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
7359a19c 341 if (IS_ERR(inode)) {
b3b94faa 342 gfs2_holder_uninit(ghs);
7359a19c 343 return PTR_ERR(inode);
b3b94faa
DT
344 }
345
5c676f6d 346 ip = ghs[1].gh_gl->gl_object;
b3b94faa
DT
347
348 ip->i_di.di_size = size;
349
350 error = gfs2_meta_inode_buffer(ip, &dibh);
351
352 if (!gfs2_assert_withdraw(sdp, !error)) {
539e5d6b 353 gfs2_dinode_out(ip, dibh->b_data);
b3b94faa
DT
354 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
b3b94faa
DT
367 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{
feaa7bba
SW
384 struct gfs2_inode *dip = GFS2_I(dir), *ip;
385 struct gfs2_sbd *sdp = GFS2_SB(dir);
b3b94faa
DT
386 struct gfs2_holder ghs[2];
387 struct inode *inode;
388 struct buffer_head *dibh;
389 int error;
390
b3b94faa
DT
391 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
392
e7f14f4d 393 inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
7359a19c 394 if (IS_ERR(inode)) {
b3b94faa 395 gfs2_holder_uninit(ghs);
7359a19c 396 return PTR_ERR(inode);
b3b94faa
DT
397 }
398
5c676f6d 399 ip = ghs[1].gh_gl->gl_object;
b3b94faa 400
4f56110a 401 ip->i_inode.i_nlink = 2;
b3b94faa
DT
402 ip->i_di.di_size = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
403 ip->i_di.di_flags |= GFS2_DIF_JDATA;
b3b94faa
DT
404 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;
c752666c 410 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
71b86f56 411 struct qstr str;
b3b94faa 412
71b86f56 413 gfs2_str2qstr(&str, ".");
c752666c
SW
414 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
415 gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
b3b94faa 416 dent->de_inum = di->di_num; /* already GFS2 endian */
7ecdb70a 417 dent->de_type = cpu_to_be16(DT_DIR);
b3b94faa
DT
418 di->di_entries = cpu_to_be32(1);
419
71b86f56 420 gfs2_str2qstr(&str, "..");
c752666c
SW
421 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);
b3b94faa 423
409e185d 424 gfs2_inum_out(&dip->i_num, &dent->de_inum);
7ecdb70a 425 dent->de_type = cpu_to_be16(DT_DIR);
b3b94faa 426
539e5d6b 427 gfs2_dinode_out(ip, di);
b3b94faa
DT
428
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
b3b94faa
DT
443 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{
feaa7bba
SW
461 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);
ddee7608
RC
464 struct gfs2_holder ghs[3];
465 struct gfs2_rgrpd *rgd;
466 struct gfs2_holder ri_gh;
b3b94faa
DT
467 int error;
468
ddee7608
RC
469
470 error = gfs2_rindex_hold(sdp, &ri_gh);
471 if (error)
472 return error;
b3b94faa
DT
473 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
ddee7608
RC
476 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);
b3b94faa
DT
480 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))
4cc14f0b 489 gfs2_dinode_print(ip);
b3b94faa
DT
490 error = -EIO;
491 goto out_gunlock;
492 }
493 if (ip->i_di.di_entries > 2) {
494 error = -ENOTEMPTY;
495 goto out_gunlock;
496 }
497
feaa7bba 498 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
b3b94faa
DT
499 if (error)
500 goto out_gunlock;
501
feaa7bba 502 error = gfs2_rmdiri(dip, &dentry->d_name, ip);
b3b94faa
DT
503
504 gfs2_trans_end(sdp);
505
a91ea69f 506out_gunlock:
ddee7608 507 gfs2_glock_dq_m(3, ghs);
a91ea69f 508out:
b3b94faa
DT
509 gfs2_holder_uninit(ghs);
510 gfs2_holder_uninit(ghs + 1);
ddee7608
RC
511 gfs2_holder_uninit(ghs + 2);
512 gfs2_glock_dq_uninit(&ri_gh);
b3b94faa
DT
513 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{
e7f14f4d 528 struct gfs2_inode *dip = GFS2_I(dir);
feaa7bba 529 struct gfs2_sbd *sdp = GFS2_SB(dir);
b3b94faa
DT
530 struct gfs2_holder ghs[2];
531 struct inode *inode;
b3b94faa
DT
532
533 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
534
e7f14f4d 535 inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
7359a19c 536 if (IS_ERR(inode)) {
b3b94faa 537 gfs2_holder_uninit(ghs);
7359a19c 538 return PTR_ERR(inode);
b3b94faa
DT
539 }
540
b3b94faa
DT
541 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
b3b94faa
DT
549 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{
feaa7bba
SW
568 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);
b3b94faa 571 struct gfs2_inode *nip = NULL;
feaa7bba 572 struct gfs2_sbd *sdp = GFS2_SB(odir);
ddee7608
RC
573 struct gfs2_holder ghs[5], r_gh;
574 struct gfs2_rgrpd *nrgd;
b3b94faa
DT
575 unsigned int num_gh;
576 int dir_rename = 0;
577 int alloc_required;
578 unsigned int x;
579 int error;
580
b3b94faa 581 if (ndentry->d_inode) {
feaa7bba 582 nip = GFS2_I(ndentry->d_inode);
b3b94faa
DT
583 if (ip == nip)
584 return 0;
585 }
586
b3b94faa
DT
587 /* Make sure we aren't trying to move a dirctory into it's subdir */
588
b60623c2 589 if (S_ISDIR(ip->i_inode.i_mode) && odip != ndip) {
b3b94faa
DT
590 dir_rename = 1;
591
b60623c2 592 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE, 0,
b3b94faa
DT
593 &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
d9d1ca30 602 num_gh = 1;
b3b94faa 603 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
d9d1ca30
SW
604 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++;
b3b94faa 610
d9d1ca30
SW
611 if (nip) {
612 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
613 num_gh++;
ddee7608
RC
614 /* 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++);
d9d1ca30 621 }
b3b94faa
DT
622
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
b60623c2 640 if (S_ISDIR(nip->i_inode.i_mode)) {
b3b94faa
DT
641 if (nip->i_di.di_entries < 2) {
642 if (gfs2_consist_inode(nip))
4cc14f0b 643 gfs2_dinode_print(nip);
b3b94faa
DT
644 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 {
faf450ef 653 error = permission(ndir, MAY_WRITE | MAY_EXEC, NULL);
b3b94faa
DT
654 if (error)
655 goto out_gunlock;
656
c752666c 657 error = gfs2_dir_search(ndir, &ndentry->d_name, NULL, NULL);
b3b94faa
DT
658 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) {
4f56110a 669 if (!ndip->i_inode.i_nlink) {
b3b94faa
DT
670 error = -EINVAL;
671 goto out_gunlock;
672 }
cd915493 673 if (ndip->i_di.di_entries == (u32)-1) {
b3b94faa
DT
674 error = -EFBIG;
675 goto out_gunlock;
676 }
b60623c2 677 if (S_ISDIR(ip->i_inode.i_mode) &&
4f56110a 678 ndip->i_inode.i_nlink == (u32)-1) {
b3b94faa
DT
679 error = -EMLINK;
680 goto out_gunlock;
681 }
682 }
683 }
684
685 /* Check out the dir to be renamed */
686
687 if (dir_rename) {
faf450ef 688 error = permission(odentry->d_inode, MAY_WRITE, NULL);
b3b94faa
DT
689 if (error)
690 goto out_gunlock;
691 }
692
c752666c
SW
693 alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
694 if (error < 0)
b3b94faa 695 goto out_gunlock;
c752666c 696 error = 0;
b3b94faa
DT
697
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
2933f925 705 error = gfs2_quota_check(ndip, ndip->i_inode.i_uid, ndip->i_inode.i_gid);
b3b94faa
DT
706 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
fe1bdedc 715 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
b3b94faa
DT
716 al->al_rgd->rd_ri.ri_length +
717 4 * RES_DINODE + 4 * RES_LEAF +
87d21e07 718 RES_STATFS + RES_QUOTA + 4, 0);
b3b94faa
DT
719 if (error)
720 goto out_ipreserv;
721 } else {
722 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
87d21e07 723 5 * RES_LEAF + 4, 0);
b3b94faa
DT
724 if (error)
725 goto out_gunlock;
726 }
727
728 /* Remove the target file, if it exists */
729
730 if (nip) {
b60623c2 731 if (S_ISDIR(nip->i_inode.i_mode))
feaa7bba
SW
732 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;
87d21e07 737 error = gfs2_change_nlink(nip, -1);
feaa7bba 738 }
b3b94faa
DT
739 if (error)
740 goto out_end_trans;
741 }
742
743 if (dir_rename) {
744 struct qstr name;
71b86f56 745 gfs2_str2qstr(&name, "..");
b3b94faa
DT
746
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;
ddfe0627 762 ip->i_inode.i_ctime = CURRENT_TIME_SEC;
d4e9c4c3 763 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
539e5d6b 764 gfs2_dinode_out(ip, dibh->b_data);
b3b94faa
DT
765 brelse(dibh);
766 }
767
768 error = gfs2_dir_del(odip, &odentry->d_name);
769 if (error)
770 goto out_end_trans;
771
c752666c 772 error = gfs2_dir_add(ndir, &ndentry->d_name, &ip->i_num,
b60623c2 773 IF2DT(ip->i_inode.i_mode));
b3b94faa
DT
774 if (error)
775 goto out_end_trans;
776
feaa7bba 777out_end_trans:
b3b94faa 778 gfs2_trans_end(sdp);
feaa7bba 779out_ipreserv:
b3b94faa
DT
780 if (alloc_required)
781 gfs2_inplace_release(ndip);
feaa7bba 782out_gunlock_q:
b3b94faa
DT
783 if (alloc_required)
784 gfs2_quota_unlock(ndip);
feaa7bba 785out_alloc:
b3b94faa
DT
786 if (alloc_required)
787 gfs2_alloc_put(ndip);
feaa7bba 788out_gunlock:
b3b94faa 789 gfs2_glock_dq_m(num_gh, ghs);
feaa7bba 790out_uninit:
b3b94faa
DT
791 for (x = 0; x < num_gh; x++)
792 gfs2_holder_uninit(ghs + x);
feaa7bba 793out_gunlock_r:
b3b94faa
DT
794 if (dir_rename)
795 gfs2_glock_dq_uninit(&r_gh);
feaa7bba 796out:
b3b94faa
DT
797 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{
feaa7bba 812 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
b3b94faa
DT
813 char array[GFS2_FAST_NAME_SIZE], *buf = array;
814 unsigned int len = GFS2_FAST_NAME_SIZE;
815 int error;
816
b3b94faa
DT
817 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{
feaa7bba 848 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
b3b94faa
DT
849 char array[GFS2_FAST_NAME_SIZE], *buf = array;
850 unsigned int len = GFS2_FAST_NAME_SIZE;
851 int error;
852
b3b94faa
DT
853 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 *
300c7d75
SW
869 * 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 *
b3b94faa
DT
873 * Returns: errno
874 */
875
876static int gfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
877{
feaa7bba 878 struct gfs2_inode *ip = GFS2_I(inode);
b3b94faa
DT
879 struct gfs2_holder i_gh;
880 int error;
300c7d75 881 int unlock = 0;
b3b94faa 882
300c7d75
SW
883 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;
888 }
b3b94faa 889
77386e1f 890 error = generic_permission(inode, mask, gfs2_check_acl);
300c7d75 891 if (unlock)
b3b94faa 892 gfs2_glock_dq_uninit(&i_gh);
b3b94faa
DT
893
894 return error;
895}
896
897static int setattr_size(struct inode *inode, struct iattr *attr)
898{
feaa7bba 899 struct gfs2_inode *ip = GFS2_I(inode);
b3b94faa
DT
900 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
aa6a85a9 908 error = gfs2_truncatei(ip, attr->ia_size);
b3b94faa
DT
909 if (error)
910 return error;
911
912 return error;
913}
914
915static int setattr_chown(struct inode *inode, struct iattr *attr)
916{
feaa7bba
SW
917 struct gfs2_inode *ip = GFS2_I(inode);
918 struct gfs2_sbd *sdp = GFS2_SB(inode);
b3b94faa 919 struct buffer_head *dibh;
cd915493 920 u32 ouid, ogid, nuid, ngid;
b3b94faa
DT
921 int error;
922
2933f925
SW
923 ouid = inode->i_uid;
924 ogid = inode->i_gid;
b3b94faa
DT
925 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);
b3b94faa 955
d4e9c4c3 956 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
539e5d6b 957 gfs2_dinode_out(ip, dibh->b_data);
b3b94faa
DT
958 brelse(dibh);
959
960 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
af18ddb8
SW
961 gfs2_quota_change(ip, -ip->i_di.di_blocks, ouid, ogid);
962 gfs2_quota_change(ip, ip->i_di.di_blocks, nuid, ngid);
b3b94faa
DT
963 }
964
a91ea69f 965out_end_trans:
b3b94faa 966 gfs2_trans_end(sdp);
a91ea69f 967out_gunlock_q:
b3b94faa 968 gfs2_quota_unlock(ip);
a91ea69f 969out_alloc:
b3b94faa 970 gfs2_alloc_put(ip);
b3b94faa
DT
971 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;
feaa7bba 988 struct gfs2_inode *ip = GFS2_I(inode);
b3b94faa
DT
989 struct gfs2_holder i_gh;
990 int error;
991
b3b94faa
DT
992 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
a91ea69f 1013out:
b3b94faa 1014 gfs2_glock_dq_uninit(&i_gh);
b3b94faa
DT
1015 if (!error)
1016 mark_inode_dirty(inode);
b3b94faa
DT
1017 return error;
1018}
1019
1020/**
1021 * gfs2_getattr - Read out an inode's attributes
26c1a574 1022 * @mnt: The vfsmount the inode is being accessed from
b3b94faa
DT
1023 * @dentry: The dentry to stat
1024 * @stat: The inode's stats
1025 *
dcf3dd85
SW
1026 * 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 *
b3b94faa
DT
1032 * 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;
feaa7bba 1039 struct gfs2_inode *ip = GFS2_I(inode);
b3b94faa
DT
1040 struct gfs2_holder gh;
1041 int error;
dcf3dd85 1042 int unlock = 0;
b3b94faa 1043
dcf3dd85
SW
1044 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;
b3b94faa
DT
1049 }
1050
dcf3dd85 1051 generic_fillattr(inode, stat);
d7c103d0 1052 if (unlock)
dcf3dd85
SW
1053 gfs2_glock_dq_uninit(&gh);
1054
1055 return 0;
b3b94faa
DT
1056}
1057
1058static int gfs2_setxattr(struct dentry *dentry, const char *name,
1059 const void *data, size_t size, int flags)
1060{
feaa7bba 1061 struct inode *inode = dentry->d_inode;
b3b94faa
DT
1062 struct gfs2_ea_request er;
1063
b3b94faa
DT
1064 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
feaa7bba 1073 gfs2_assert_warn(GFS2_SB(inode), !(er.er_flags & GFS2_ERF_MODE));
b3b94faa 1074
feaa7bba 1075 return gfs2_ea_set(GFS2_I(inode), &er);
b3b94faa
DT
1076}
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
b3b94faa
DT
1083 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
feaa7bba 1091 return gfs2_ea_get(GFS2_I(dentry->d_inode), &er);
b3b94faa
DT
1092}
1093
1094static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
1095{
1096 struct gfs2_ea_request er;
1097
b3b94faa
DT
1098 memset(&er, 0, sizeof(struct gfs2_ea_request));
1099 er.er_data = (size) ? buffer : NULL;
1100 er.er_data_len = size;
1101
feaa7bba 1102 return gfs2_ea_list(GFS2_I(dentry->d_inode), &er);
b3b94faa
DT
1103}
1104
1105static int gfs2_removexattr(struct dentry *dentry, const char *name)
1106{
1107 struct gfs2_ea_request er;
1108
b3b94faa
DT
1109 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
feaa7bba 1115 return gfs2_ea_remove(GFS2_I(dentry->d_inode), &er);
b3b94faa
DT
1116}
1117
92e1d5be 1118const struct inode_operations gfs2_file_iops = {
b3b94faa
DT
1119 .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
92e1d5be 1128const struct inode_operations gfs2_dev_iops = {
b3b94faa
DT
1129 .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
92e1d5be 1138const struct inode_operations gfs2_dir_iops = {
b3b94faa
DT
1139 .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
92e1d5be 1157const struct inode_operations gfs2_symlink_iops = {
b3b94faa
DT
1158 .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
This page took 0.345663 seconds and 5 git commands to generate.