]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - fs/gfs2/ops_inode.c
Merge branch 'for-2.6.30' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[mirror_ubuntu-hirsute-kernel.git] / fs / gfs2 / ops_inode.c
1 /*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
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
7 * of the GNU General Public License version 2.
8 */
9
10 #include <linux/slab.h>
11 #include <linux/spinlock.h>
12 #include <linux/completion.h>
13 #include <linux/buffer_head.h>
14 #include <linux/namei.h>
15 #include <linux/utsname.h>
16 #include <linux/mm.h>
17 #include <linux/xattr.h>
18 #include <linux/posix_acl.h>
19 #include <linux/gfs2_ondisk.h>
20 #include <linux/crc32.h>
21 #include <linux/fiemap.h>
22 #include <asm/uaccess.h>
23
24 #include "gfs2.h"
25 #include "incore.h"
26 #include "acl.h"
27 #include "bmap.h"
28 #include "dir.h"
29 #include "eaops.h"
30 #include "eattr.h"
31 #include "glock.h"
32 #include "inode.h"
33 #include "meta_io.h"
34 #include "quota.h"
35 #include "rgrp.h"
36 #include "trans.h"
37 #include "util.h"
38 #include "super.h"
39
40 /**
41 * gfs2_create - Create a file
42 * @dir: The directory in which to create the file
43 * @dentry: The dentry of the new file
44 * @mode: The mode of the new file
45 *
46 * Returns: errno
47 */
48
49 static int gfs2_create(struct inode *dir, struct dentry *dentry,
50 int mode, struct nameidata *nd)
51 {
52 struct gfs2_inode *dip = GFS2_I(dir);
53 struct gfs2_sbd *sdp = GFS2_SB(dir);
54 struct gfs2_holder ghs[2];
55 struct inode *inode;
56
57 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
58
59 for (;;) {
60 inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0);
61 if (!IS_ERR(inode)) {
62 gfs2_trans_end(sdp);
63 if (dip->i_alloc->al_rgd)
64 gfs2_inplace_release(dip);
65 gfs2_quota_unlock(dip);
66 gfs2_alloc_put(dip);
67 gfs2_glock_dq_uninit_m(2, ghs);
68 mark_inode_dirty(inode);
69 break;
70 } else if (PTR_ERR(inode) != -EEXIST ||
71 (nd && nd->flags & LOOKUP_EXCL)) {
72 gfs2_holder_uninit(ghs);
73 return PTR_ERR(inode);
74 }
75
76 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
77 if (inode) {
78 if (!IS_ERR(inode)) {
79 gfs2_holder_uninit(ghs);
80 break;
81 } else {
82 gfs2_holder_uninit(ghs);
83 return PTR_ERR(inode);
84 }
85 }
86 }
87
88 d_instantiate(dentry, inode);
89
90 return 0;
91 }
92
93 /**
94 * gfs2_lookup - Look up a filename in a directory and return its inode
95 * @dir: The directory inode
96 * @dentry: The dentry of the new inode
97 * @nd: passed from Linux VFS, ignored by us
98 *
99 * Called by the VFS layer. Lock dir and call gfs2_lookupi()
100 *
101 * Returns: errno
102 */
103
104 static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
105 struct nameidata *nd)
106 {
107 struct inode *inode = NULL;
108
109 dentry->d_op = &gfs2_dops;
110
111 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
112 if (inode && IS_ERR(inode))
113 return ERR_CAST(inode);
114
115 if (inode) {
116 struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
117 struct gfs2_holder gh;
118 int error;
119 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
120 if (error) {
121 iput(inode);
122 return ERR_PTR(error);
123 }
124 gfs2_glock_dq_uninit(&gh);
125 return d_splice_alias(inode, dentry);
126 }
127 d_add(dentry, inode);
128
129 return NULL;
130 }
131
132 /**
133 * gfs2_link - Link to a file
134 * @old_dentry: The inode to link
135 * @dir: Add link to this directory
136 * @dentry: The name of the link
137 *
138 * Link the inode in "old_dentry" into the directory "dir" with the
139 * name in "dentry".
140 *
141 * Returns: errno
142 */
143
144 static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
145 struct dentry *dentry)
146 {
147 struct gfs2_inode *dip = GFS2_I(dir);
148 struct gfs2_sbd *sdp = GFS2_SB(dir);
149 struct inode *inode = old_dentry->d_inode;
150 struct gfs2_inode *ip = GFS2_I(inode);
151 struct gfs2_holder ghs[2];
152 int alloc_required;
153 int error;
154
155 if (S_ISDIR(inode->i_mode))
156 return -EPERM;
157
158 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
159 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
160
161 error = gfs2_glock_nq(ghs); /* parent */
162 if (error)
163 goto out_parent;
164
165 error = gfs2_glock_nq(ghs + 1); /* child */
166 if (error)
167 goto out_child;
168
169 error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
170 if (error)
171 goto out_gunlock;
172
173 error = gfs2_dir_check(dir, &dentry->d_name, NULL);
174 switch (error) {
175 case -ENOENT:
176 break;
177 case 0:
178 error = -EEXIST;
179 default:
180 goto out_gunlock;
181 }
182
183 error = -EINVAL;
184 if (!dip->i_inode.i_nlink)
185 goto out_gunlock;
186 error = -EFBIG;
187 if (dip->i_entries == (u32)-1)
188 goto out_gunlock;
189 error = -EPERM;
190 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
191 goto out_gunlock;
192 error = -EINVAL;
193 if (!ip->i_inode.i_nlink)
194 goto out_gunlock;
195 error = -EMLINK;
196 if (ip->i_inode.i_nlink == (u32)-1)
197 goto out_gunlock;
198
199 alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
200 if (error < 0)
201 goto out_gunlock;
202 error = 0;
203
204 if (alloc_required) {
205 struct gfs2_alloc *al = gfs2_alloc_get(dip);
206 if (!al) {
207 error = -ENOMEM;
208 goto out_gunlock;
209 }
210
211 error = gfs2_quota_lock_check(dip);
212 if (error)
213 goto out_alloc;
214
215 al->al_requested = sdp->sd_max_dirres;
216
217 error = gfs2_inplace_reserve(dip);
218 if (error)
219 goto out_gunlock_q;
220
221 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
222 al->al_rgd->rd_length +
223 2 * RES_DINODE + RES_STATFS +
224 RES_QUOTA, 0);
225 if (error)
226 goto out_ipres;
227 } else {
228 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
229 if (error)
230 goto out_ipres;
231 }
232
233 error = gfs2_dir_add(dir, &dentry->d_name, ip, IF2DT(inode->i_mode));
234 if (error)
235 goto out_end_trans;
236
237 error = gfs2_change_nlink(ip, +1);
238
239 out_end_trans:
240 gfs2_trans_end(sdp);
241 out_ipres:
242 if (alloc_required)
243 gfs2_inplace_release(dip);
244 out_gunlock_q:
245 if (alloc_required)
246 gfs2_quota_unlock(dip);
247 out_alloc:
248 if (alloc_required)
249 gfs2_alloc_put(dip);
250 out_gunlock:
251 gfs2_glock_dq(ghs + 1);
252 out_child:
253 gfs2_glock_dq(ghs);
254 out_parent:
255 gfs2_holder_uninit(ghs);
256 gfs2_holder_uninit(ghs + 1);
257 if (!error) {
258 atomic_inc(&inode->i_count);
259 d_instantiate(dentry, inode);
260 mark_inode_dirty(inode);
261 }
262 return error;
263 }
264
265 /**
266 * gfs2_unlink - Unlink a file
267 * @dir: The inode of the directory containing the file to unlink
268 * @dentry: The file itself
269 *
270 * Unlink a file. Call gfs2_unlinki()
271 *
272 * Returns: errno
273 */
274
275 static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
276 {
277 struct gfs2_inode *dip = GFS2_I(dir);
278 struct gfs2_sbd *sdp = GFS2_SB(dir);
279 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
280 struct gfs2_holder ghs[3];
281 struct gfs2_rgrpd *rgd;
282 struct gfs2_holder ri_gh;
283 int error;
284
285 error = gfs2_rindex_hold(sdp, &ri_gh);
286 if (error)
287 return error;
288
289 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
290 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
291
292 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
293 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
294
295
296 error = gfs2_glock_nq(ghs); /* parent */
297 if (error)
298 goto out_parent;
299
300 error = gfs2_glock_nq(ghs + 1); /* child */
301 if (error)
302 goto out_child;
303
304 error = gfs2_glock_nq(ghs + 2); /* rgrp */
305 if (error)
306 goto out_rgrp;
307
308 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
309 if (error)
310 goto out_gunlock;
311
312 error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
313 if (error)
314 goto out_rgrp;
315
316 error = gfs2_dir_del(dip, &dentry->d_name);
317 if (error)
318 goto out_end_trans;
319
320 error = gfs2_change_nlink(ip, -1);
321
322 out_end_trans:
323 gfs2_trans_end(sdp);
324 out_gunlock:
325 gfs2_glock_dq(ghs + 2);
326 out_rgrp:
327 gfs2_holder_uninit(ghs + 2);
328 gfs2_glock_dq(ghs + 1);
329 out_child:
330 gfs2_holder_uninit(ghs + 1);
331 gfs2_glock_dq(ghs);
332 out_parent:
333 gfs2_holder_uninit(ghs);
334 gfs2_glock_dq_uninit(&ri_gh);
335 return error;
336 }
337
338 /**
339 * gfs2_symlink - Create a symlink
340 * @dir: The directory to create the symlink in
341 * @dentry: The dentry to put the symlink in
342 * @symname: The thing which the link points to
343 *
344 * Returns: errno
345 */
346
347 static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
348 const char *symname)
349 {
350 struct gfs2_inode *dip = GFS2_I(dir), *ip;
351 struct gfs2_sbd *sdp = GFS2_SB(dir);
352 struct gfs2_holder ghs[2];
353 struct inode *inode;
354 struct buffer_head *dibh;
355 int size;
356 int error;
357
358 /* Must be stuffed with a null terminator for gfs2_follow_link() */
359 size = strlen(symname);
360 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
361 return -ENAMETOOLONG;
362
363 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
364
365 inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
366 if (IS_ERR(inode)) {
367 gfs2_holder_uninit(ghs);
368 return PTR_ERR(inode);
369 }
370
371 ip = ghs[1].gh_gl->gl_object;
372
373 ip->i_disksize = size;
374
375 error = gfs2_meta_inode_buffer(ip, &dibh);
376
377 if (!gfs2_assert_withdraw(sdp, !error)) {
378 gfs2_dinode_out(ip, dibh->b_data);
379 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
380 size);
381 brelse(dibh);
382 }
383
384 gfs2_trans_end(sdp);
385 if (dip->i_alloc->al_rgd)
386 gfs2_inplace_release(dip);
387 gfs2_quota_unlock(dip);
388 gfs2_alloc_put(dip);
389
390 gfs2_glock_dq_uninit_m(2, ghs);
391
392 d_instantiate(dentry, inode);
393 mark_inode_dirty(inode);
394
395 return 0;
396 }
397
398 /**
399 * gfs2_mkdir - Make a directory
400 * @dir: The parent directory of the new one
401 * @dentry: The dentry of the new directory
402 * @mode: The mode of the new directory
403 *
404 * Returns: errno
405 */
406
407 static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
408 {
409 struct gfs2_inode *dip = GFS2_I(dir), *ip;
410 struct gfs2_sbd *sdp = GFS2_SB(dir);
411 struct gfs2_holder ghs[2];
412 struct inode *inode;
413 struct buffer_head *dibh;
414 int error;
415
416 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
417
418 inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
419 if (IS_ERR(inode)) {
420 gfs2_holder_uninit(ghs);
421 return PTR_ERR(inode);
422 }
423
424 ip = ghs[1].gh_gl->gl_object;
425
426 ip->i_inode.i_nlink = 2;
427 ip->i_disksize = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
428 ip->i_diskflags |= GFS2_DIF_JDATA;
429 ip->i_entries = 2;
430
431 error = gfs2_meta_inode_buffer(ip, &dibh);
432
433 if (!gfs2_assert_withdraw(sdp, !error)) {
434 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
435 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
436 struct qstr str;
437
438 gfs2_str2qstr(&str, ".");
439 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
440 gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
441 dent->de_inum = di->di_num; /* already GFS2 endian */
442 dent->de_type = cpu_to_be16(DT_DIR);
443 di->di_entries = cpu_to_be32(1);
444
445 gfs2_str2qstr(&str, "..");
446 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
447 gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
448
449 gfs2_inum_out(dip, dent);
450 dent->de_type = cpu_to_be16(DT_DIR);
451
452 gfs2_dinode_out(ip, di);
453
454 brelse(dibh);
455 }
456
457 error = gfs2_change_nlink(dip, +1);
458 gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
459
460 gfs2_trans_end(sdp);
461 if (dip->i_alloc->al_rgd)
462 gfs2_inplace_release(dip);
463 gfs2_quota_unlock(dip);
464 gfs2_alloc_put(dip);
465
466 gfs2_glock_dq_uninit_m(2, ghs);
467
468 d_instantiate(dentry, inode);
469 mark_inode_dirty(inode);
470
471 return 0;
472 }
473
474 /**
475 * gfs2_rmdir - Remove a directory
476 * @dir: The parent directory of the directory to be removed
477 * @dentry: The dentry of the directory to remove
478 *
479 * Remove a directory. Call gfs2_rmdiri()
480 *
481 * Returns: errno
482 */
483
484 static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
485 {
486 struct gfs2_inode *dip = GFS2_I(dir);
487 struct gfs2_sbd *sdp = GFS2_SB(dir);
488 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
489 struct gfs2_holder ghs[3];
490 struct gfs2_rgrpd *rgd;
491 struct gfs2_holder ri_gh;
492 int error;
493
494 error = gfs2_rindex_hold(sdp, &ri_gh);
495 if (error)
496 return error;
497 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
498 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
499
500 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
501 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
502
503 error = gfs2_glock_nq(ghs); /* parent */
504 if (error)
505 goto out_parent;
506
507 error = gfs2_glock_nq(ghs + 1); /* child */
508 if (error)
509 goto out_child;
510
511 error = gfs2_glock_nq(ghs + 2); /* rgrp */
512 if (error)
513 goto out_rgrp;
514
515 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
516 if (error)
517 goto out_gunlock;
518
519 if (ip->i_entries < 2) {
520 if (gfs2_consist_inode(ip))
521 gfs2_dinode_print(ip);
522 error = -EIO;
523 goto out_gunlock;
524 }
525 if (ip->i_entries > 2) {
526 error = -ENOTEMPTY;
527 goto out_gunlock;
528 }
529
530 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
531 if (error)
532 goto out_gunlock;
533
534 error = gfs2_rmdiri(dip, &dentry->d_name, ip);
535
536 gfs2_trans_end(sdp);
537
538 out_gunlock:
539 gfs2_glock_dq(ghs + 2);
540 out_rgrp:
541 gfs2_holder_uninit(ghs + 2);
542 gfs2_glock_dq(ghs + 1);
543 out_child:
544 gfs2_holder_uninit(ghs + 1);
545 gfs2_glock_dq(ghs);
546 out_parent:
547 gfs2_holder_uninit(ghs);
548 gfs2_glock_dq_uninit(&ri_gh);
549 return error;
550 }
551
552 /**
553 * gfs2_mknod - Make a special file
554 * @dir: The directory in which the special file will reside
555 * @dentry: The dentry of the special file
556 * @mode: The mode of the special file
557 * @rdev: The device specification of the special file
558 *
559 */
560
561 static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
562 dev_t dev)
563 {
564 struct gfs2_inode *dip = GFS2_I(dir);
565 struct gfs2_sbd *sdp = GFS2_SB(dir);
566 struct gfs2_holder ghs[2];
567 struct inode *inode;
568
569 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
570
571 inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
572 if (IS_ERR(inode)) {
573 gfs2_holder_uninit(ghs);
574 return PTR_ERR(inode);
575 }
576
577 gfs2_trans_end(sdp);
578 if (dip->i_alloc->al_rgd)
579 gfs2_inplace_release(dip);
580 gfs2_quota_unlock(dip);
581 gfs2_alloc_put(dip);
582
583 gfs2_glock_dq_uninit_m(2, ghs);
584
585 d_instantiate(dentry, inode);
586 mark_inode_dirty(inode);
587
588 return 0;
589 }
590
591 /*
592 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
593 * @this: move this
594 * @to: to here
595 *
596 * Follow @to back to the root and make sure we don't encounter @this
597 * Assumes we already hold the rename lock.
598 *
599 * Returns: errno
600 */
601
602 static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
603 {
604 struct inode *dir = &to->i_inode;
605 struct super_block *sb = dir->i_sb;
606 struct inode *tmp;
607 struct qstr dotdot;
608 int error = 0;
609
610 gfs2_str2qstr(&dotdot, "..");
611
612 igrab(dir);
613
614 for (;;) {
615 if (dir == &this->i_inode) {
616 error = -EINVAL;
617 break;
618 }
619 if (dir == sb->s_root->d_inode) {
620 error = 0;
621 break;
622 }
623
624 tmp = gfs2_lookupi(dir, &dotdot, 1);
625 if (IS_ERR(tmp)) {
626 error = PTR_ERR(tmp);
627 break;
628 }
629
630 iput(dir);
631 dir = tmp;
632 }
633
634 iput(dir);
635
636 return error;
637 }
638
639 /**
640 * gfs2_rename - Rename a file
641 * @odir: Parent directory of old file name
642 * @odentry: The old dentry of the file
643 * @ndir: Parent directory of new file name
644 * @ndentry: The new dentry of the file
645 *
646 * Returns: errno
647 */
648
649 static int gfs2_rename(struct inode *odir, struct dentry *odentry,
650 struct inode *ndir, struct dentry *ndentry)
651 {
652 struct gfs2_inode *odip = GFS2_I(odir);
653 struct gfs2_inode *ndip = GFS2_I(ndir);
654 struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
655 struct gfs2_inode *nip = NULL;
656 struct gfs2_sbd *sdp = GFS2_SB(odir);
657 struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, };
658 struct gfs2_rgrpd *nrgd;
659 unsigned int num_gh;
660 int dir_rename = 0;
661 int alloc_required;
662 unsigned int x;
663 int error;
664
665 if (ndentry->d_inode) {
666 nip = GFS2_I(ndentry->d_inode);
667 if (ip == nip)
668 return 0;
669 }
670
671
672 if (odip != ndip) {
673 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
674 0, &r_gh);
675 if (error)
676 goto out;
677
678 if (S_ISDIR(ip->i_inode.i_mode)) {
679 dir_rename = 1;
680 /* don't move a dirctory into it's subdir */
681 error = gfs2_ok_to_move(ip, ndip);
682 if (error)
683 goto out_gunlock_r;
684 }
685 }
686
687 num_gh = 1;
688 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
689 if (odip != ndip) {
690 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
691 num_gh++;
692 }
693 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
694 num_gh++;
695
696 if (nip) {
697 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
698 num_gh++;
699 /* grab the resource lock for unlink flag twiddling
700 * this is the case of the target file already existing
701 * so we unlink before doing the rename
702 */
703 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
704 if (nrgd)
705 gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
706 }
707
708 for (x = 0; x < num_gh; x++) {
709 error = gfs2_glock_nq(ghs + x);
710 if (error)
711 goto out_gunlock;
712 }
713
714 /* Check out the old directory */
715
716 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
717 if (error)
718 goto out_gunlock;
719
720 /* Check out the new directory */
721
722 if (nip) {
723 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
724 if (error)
725 goto out_gunlock;
726
727 if (S_ISDIR(nip->i_inode.i_mode)) {
728 if (nip->i_entries < 2) {
729 if (gfs2_consist_inode(nip))
730 gfs2_dinode_print(nip);
731 error = -EIO;
732 goto out_gunlock;
733 }
734 if (nip->i_entries > 2) {
735 error = -ENOTEMPTY;
736 goto out_gunlock;
737 }
738 }
739 } else {
740 error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
741 if (error)
742 goto out_gunlock;
743
744 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
745 switch (error) {
746 case -ENOENT:
747 error = 0;
748 break;
749 case 0:
750 error = -EEXIST;
751 default:
752 goto out_gunlock;
753 };
754
755 if (odip != ndip) {
756 if (!ndip->i_inode.i_nlink) {
757 error = -EINVAL;
758 goto out_gunlock;
759 }
760 if (ndip->i_entries == (u32)-1) {
761 error = -EFBIG;
762 goto out_gunlock;
763 }
764 if (S_ISDIR(ip->i_inode.i_mode) &&
765 ndip->i_inode.i_nlink == (u32)-1) {
766 error = -EMLINK;
767 goto out_gunlock;
768 }
769 }
770 }
771
772 /* Check out the dir to be renamed */
773
774 if (dir_rename) {
775 error = gfs2_permission(odentry->d_inode, MAY_WRITE);
776 if (error)
777 goto out_gunlock;
778 }
779
780 alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
781 if (error < 0)
782 goto out_gunlock;
783 error = 0;
784
785 if (alloc_required) {
786 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
787 if (!al) {
788 error = -ENOMEM;
789 goto out_gunlock;
790 }
791
792 error = gfs2_quota_lock_check(ndip);
793 if (error)
794 goto out_alloc;
795
796 al->al_requested = sdp->sd_max_dirres;
797
798 error = gfs2_inplace_reserve(ndip);
799 if (error)
800 goto out_gunlock_q;
801
802 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
803 al->al_rgd->rd_length +
804 4 * RES_DINODE + 4 * RES_LEAF +
805 RES_STATFS + RES_QUOTA + 4, 0);
806 if (error)
807 goto out_ipreserv;
808 } else {
809 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
810 5 * RES_LEAF + 4, 0);
811 if (error)
812 goto out_gunlock;
813 }
814
815 /* Remove the target file, if it exists */
816
817 if (nip) {
818 if (S_ISDIR(nip->i_inode.i_mode))
819 error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
820 else {
821 error = gfs2_dir_del(ndip, &ndentry->d_name);
822 if (error)
823 goto out_end_trans;
824 error = gfs2_change_nlink(nip, -1);
825 }
826 if (error)
827 goto out_end_trans;
828 }
829
830 if (dir_rename) {
831 struct qstr name;
832 gfs2_str2qstr(&name, "..");
833
834 error = gfs2_change_nlink(ndip, +1);
835 if (error)
836 goto out_end_trans;
837 error = gfs2_change_nlink(odip, -1);
838 if (error)
839 goto out_end_trans;
840
841 error = gfs2_dir_mvino(ip, &name, ndip, DT_DIR);
842 if (error)
843 goto out_end_trans;
844 } else {
845 struct buffer_head *dibh;
846 error = gfs2_meta_inode_buffer(ip, &dibh);
847 if (error)
848 goto out_end_trans;
849 ip->i_inode.i_ctime = CURRENT_TIME;
850 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
851 gfs2_dinode_out(ip, dibh->b_data);
852 brelse(dibh);
853 }
854
855 error = gfs2_dir_del(odip, &odentry->d_name);
856 if (error)
857 goto out_end_trans;
858
859 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
860 if (error)
861 goto out_end_trans;
862
863 out_end_trans:
864 gfs2_trans_end(sdp);
865 out_ipreserv:
866 if (alloc_required)
867 gfs2_inplace_release(ndip);
868 out_gunlock_q:
869 if (alloc_required)
870 gfs2_quota_unlock(ndip);
871 out_alloc:
872 if (alloc_required)
873 gfs2_alloc_put(ndip);
874 out_gunlock:
875 while (x--) {
876 gfs2_glock_dq(ghs + x);
877 gfs2_holder_uninit(ghs + x);
878 }
879 out_gunlock_r:
880 if (r_gh.gh_gl)
881 gfs2_glock_dq_uninit(&r_gh);
882 out:
883 return error;
884 }
885
886 /**
887 * gfs2_readlink - Read the value of a symlink
888 * @dentry: the symlink
889 * @buf: the buffer to read the symlink data into
890 * @size: the size of the buffer
891 *
892 * Returns: errno
893 */
894
895 static int gfs2_readlink(struct dentry *dentry, char __user *user_buf,
896 int user_size)
897 {
898 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
899 char array[GFS2_FAST_NAME_SIZE], *buf = array;
900 unsigned int len = GFS2_FAST_NAME_SIZE;
901 int error;
902
903 error = gfs2_readlinki(ip, &buf, &len);
904 if (error)
905 return error;
906
907 if (user_size > len - 1)
908 user_size = len - 1;
909
910 if (copy_to_user(user_buf, buf, user_size))
911 error = -EFAULT;
912 else
913 error = user_size;
914
915 if (buf != array)
916 kfree(buf);
917
918 return error;
919 }
920
921 /**
922 * gfs2_follow_link - Follow a symbolic link
923 * @dentry: The dentry of the link
924 * @nd: Data that we pass to vfs_follow_link()
925 *
926 * This can handle symlinks of any size. It is optimised for symlinks
927 * under GFS2_FAST_NAME_SIZE.
928 *
929 * Returns: 0 on success or error code
930 */
931
932 static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
933 {
934 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
935 char array[GFS2_FAST_NAME_SIZE], *buf = array;
936 unsigned int len = GFS2_FAST_NAME_SIZE;
937 int error;
938
939 error = gfs2_readlinki(ip, &buf, &len);
940 if (!error) {
941 error = vfs_follow_link(nd, buf);
942 if (buf != array)
943 kfree(buf);
944 }
945
946 return ERR_PTR(error);
947 }
948
949 /**
950 * gfs2_permission -
951 * @inode:
952 * @mask:
953 * @nd: passed from Linux VFS, ignored by us
954 *
955 * This may be called from the VFS directly, or from within GFS2 with the
956 * inode locked, so we look to see if the glock is already locked and only
957 * lock the glock if its not already been done.
958 *
959 * Returns: errno
960 */
961
962 int gfs2_permission(struct inode *inode, int mask)
963 {
964 struct gfs2_inode *ip = GFS2_I(inode);
965 struct gfs2_holder i_gh;
966 int error;
967 int unlock = 0;
968
969 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
970 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
971 if (error)
972 return error;
973 unlock = 1;
974 }
975
976 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
977 error = -EACCES;
978 else
979 error = generic_permission(inode, mask, gfs2_check_acl);
980 if (unlock)
981 gfs2_glock_dq_uninit(&i_gh);
982
983 return error;
984 }
985
986 static int setattr_size(struct inode *inode, struct iattr *attr)
987 {
988 struct gfs2_inode *ip = GFS2_I(inode);
989 struct gfs2_sbd *sdp = GFS2_SB(inode);
990 int error;
991
992 if (attr->ia_size != ip->i_disksize) {
993 error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
994 if (error)
995 return error;
996 error = vmtruncate(inode, attr->ia_size);
997 gfs2_trans_end(sdp);
998 if (error)
999 return error;
1000 }
1001
1002 error = gfs2_truncatei(ip, attr->ia_size);
1003 if (error && (inode->i_size != ip->i_disksize))
1004 i_size_write(inode, ip->i_disksize);
1005
1006 return error;
1007 }
1008
1009 static int setattr_chown(struct inode *inode, struct iattr *attr)
1010 {
1011 struct gfs2_inode *ip = GFS2_I(inode);
1012 struct gfs2_sbd *sdp = GFS2_SB(inode);
1013 struct buffer_head *dibh;
1014 u32 ouid, ogid, nuid, ngid;
1015 int error;
1016
1017 ouid = inode->i_uid;
1018 ogid = inode->i_gid;
1019 nuid = attr->ia_uid;
1020 ngid = attr->ia_gid;
1021
1022 if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
1023 ouid = nuid = NO_QUOTA_CHANGE;
1024 if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
1025 ogid = ngid = NO_QUOTA_CHANGE;
1026
1027 if (!gfs2_alloc_get(ip))
1028 return -ENOMEM;
1029
1030 error = gfs2_quota_lock(ip, nuid, ngid);
1031 if (error)
1032 goto out_alloc;
1033
1034 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1035 error = gfs2_quota_check(ip, nuid, ngid);
1036 if (error)
1037 goto out_gunlock_q;
1038 }
1039
1040 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
1041 if (error)
1042 goto out_gunlock_q;
1043
1044 error = gfs2_meta_inode_buffer(ip, &dibh);
1045 if (error)
1046 goto out_end_trans;
1047
1048 error = inode_setattr(inode, attr);
1049 gfs2_assert_warn(sdp, !error);
1050
1051 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1052 gfs2_dinode_out(ip, dibh->b_data);
1053 brelse(dibh);
1054
1055 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1056 u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
1057 gfs2_quota_change(ip, -blocks, ouid, ogid);
1058 gfs2_quota_change(ip, blocks, nuid, ngid);
1059 }
1060
1061 out_end_trans:
1062 gfs2_trans_end(sdp);
1063 out_gunlock_q:
1064 gfs2_quota_unlock(ip);
1065 out_alloc:
1066 gfs2_alloc_put(ip);
1067 return error;
1068 }
1069
1070 /**
1071 * gfs2_setattr - Change attributes on an inode
1072 * @dentry: The dentry which is changing
1073 * @attr: The structure describing the change
1074 *
1075 * The VFS layer wants to change one or more of an inodes attributes. Write
1076 * that change out to disk.
1077 *
1078 * Returns: errno
1079 */
1080
1081 static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
1082 {
1083 struct inode *inode = dentry->d_inode;
1084 struct gfs2_inode *ip = GFS2_I(inode);
1085 struct gfs2_holder i_gh;
1086 int error;
1087
1088 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1089 if (error)
1090 return error;
1091
1092 error = -EPERM;
1093 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1094 goto out;
1095
1096 error = inode_change_ok(inode, attr);
1097 if (error)
1098 goto out;
1099
1100 if (attr->ia_valid & ATTR_SIZE)
1101 error = setattr_size(inode, attr);
1102 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1103 error = setattr_chown(inode, attr);
1104 else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1105 error = gfs2_acl_chmod(ip, attr);
1106 else
1107 error = gfs2_setattr_simple(ip, attr);
1108
1109 out:
1110 gfs2_glock_dq_uninit(&i_gh);
1111 if (!error)
1112 mark_inode_dirty(inode);
1113 return error;
1114 }
1115
1116 /**
1117 * gfs2_getattr - Read out an inode's attributes
1118 * @mnt: The vfsmount the inode is being accessed from
1119 * @dentry: The dentry to stat
1120 * @stat: The inode's stats
1121 *
1122 * This may be called from the VFS directly, or from within GFS2 with the
1123 * inode locked, so we look to see if the glock is already locked and only
1124 * lock the glock if its not already been done. Note that its the NFS
1125 * readdirplus operation which causes this to be called (from filldir)
1126 * with the glock already held.
1127 *
1128 * Returns: errno
1129 */
1130
1131 static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1132 struct kstat *stat)
1133 {
1134 struct inode *inode = dentry->d_inode;
1135 struct gfs2_inode *ip = GFS2_I(inode);
1136 struct gfs2_holder gh;
1137 int error;
1138 int unlock = 0;
1139
1140 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
1141 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1142 if (error)
1143 return error;
1144 unlock = 1;
1145 }
1146
1147 generic_fillattr(inode, stat);
1148 if (unlock)
1149 gfs2_glock_dq_uninit(&gh);
1150
1151 return 0;
1152 }
1153
1154 static int gfs2_setxattr(struct dentry *dentry, const char *name,
1155 const void *data, size_t size, int flags)
1156 {
1157 struct inode *inode = dentry->d_inode;
1158 struct gfs2_ea_request er;
1159
1160 memset(&er, 0, sizeof(struct gfs2_ea_request));
1161 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1162 if (er.er_type == GFS2_EATYPE_UNUSED)
1163 return -EOPNOTSUPP;
1164 er.er_data = (char *)data;
1165 er.er_name_len = strlen(er.er_name);
1166 er.er_data_len = size;
1167 er.er_flags = flags;
1168
1169 gfs2_assert_warn(GFS2_SB(inode), !(er.er_flags & GFS2_ERF_MODE));
1170
1171 return gfs2_ea_set(GFS2_I(inode), &er);
1172 }
1173
1174 static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1175 void *data, size_t size)
1176 {
1177 struct gfs2_ea_request er;
1178
1179 memset(&er, 0, sizeof(struct gfs2_ea_request));
1180 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1181 if (er.er_type == GFS2_EATYPE_UNUSED)
1182 return -EOPNOTSUPP;
1183 er.er_data = data;
1184 er.er_name_len = strlen(er.er_name);
1185 er.er_data_len = size;
1186
1187 return gfs2_ea_get(GFS2_I(dentry->d_inode), &er);
1188 }
1189
1190 static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
1191 {
1192 struct gfs2_ea_request er;
1193
1194 memset(&er, 0, sizeof(struct gfs2_ea_request));
1195 er.er_data = (size) ? buffer : NULL;
1196 er.er_data_len = size;
1197
1198 return gfs2_ea_list(GFS2_I(dentry->d_inode), &er);
1199 }
1200
1201 static int gfs2_removexattr(struct dentry *dentry, const char *name)
1202 {
1203 struct gfs2_ea_request er;
1204
1205 memset(&er, 0, sizeof(struct gfs2_ea_request));
1206 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1207 if (er.er_type == GFS2_EATYPE_UNUSED)
1208 return -EOPNOTSUPP;
1209 er.er_name_len = strlen(er.er_name);
1210
1211 return gfs2_ea_remove(GFS2_I(dentry->d_inode), &er);
1212 }
1213
1214 static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1215 u64 start, u64 len)
1216 {
1217 struct gfs2_inode *ip = GFS2_I(inode);
1218 struct gfs2_holder gh;
1219 int ret;
1220
1221 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1222 if (ret)
1223 return ret;
1224
1225 mutex_lock(&inode->i_mutex);
1226
1227 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
1228 if (ret)
1229 goto out;
1230
1231 if (gfs2_is_stuffed(ip)) {
1232 u64 phys = ip->i_no_addr << inode->i_blkbits;
1233 u64 size = i_size_read(inode);
1234 u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
1235 FIEMAP_EXTENT_DATA_INLINE;
1236 phys += sizeof(struct gfs2_dinode);
1237 phys += start;
1238 if (start + len > size)
1239 len = size - start;
1240 if (start < size)
1241 ret = fiemap_fill_next_extent(fieinfo, start, phys,
1242 len, flags);
1243 if (ret == 1)
1244 ret = 0;
1245 } else {
1246 ret = __generic_block_fiemap(inode, fieinfo, start, len,
1247 gfs2_block_map);
1248 }
1249
1250 gfs2_glock_dq_uninit(&gh);
1251 out:
1252 mutex_unlock(&inode->i_mutex);
1253 return ret;
1254 }
1255
1256 const struct inode_operations gfs2_file_iops = {
1257 .permission = gfs2_permission,
1258 .setattr = gfs2_setattr,
1259 .getattr = gfs2_getattr,
1260 .setxattr = gfs2_setxattr,
1261 .getxattr = gfs2_getxattr,
1262 .listxattr = gfs2_listxattr,
1263 .removexattr = gfs2_removexattr,
1264 .fiemap = gfs2_fiemap,
1265 };
1266
1267 const struct inode_operations gfs2_dir_iops = {
1268 .create = gfs2_create,
1269 .lookup = gfs2_lookup,
1270 .link = gfs2_link,
1271 .unlink = gfs2_unlink,
1272 .symlink = gfs2_symlink,
1273 .mkdir = gfs2_mkdir,
1274 .rmdir = gfs2_rmdir,
1275 .mknod = gfs2_mknod,
1276 .rename = gfs2_rename,
1277 .permission = gfs2_permission,
1278 .setattr = gfs2_setattr,
1279 .getattr = gfs2_getattr,
1280 .setxattr = gfs2_setxattr,
1281 .getxattr = gfs2_getxattr,
1282 .listxattr = gfs2_listxattr,
1283 .removexattr = gfs2_removexattr,
1284 .fiemap = gfs2_fiemap,
1285 };
1286
1287 const struct inode_operations gfs2_symlink_iops = {
1288 .readlink = gfs2_readlink,
1289 .follow_link = gfs2_follow_link,
1290 .permission = gfs2_permission,
1291 .setattr = gfs2_setattr,
1292 .getattr = gfs2_getattr,
1293 .setxattr = gfs2_setxattr,
1294 .getxattr = gfs2_getxattr,
1295 .listxattr = gfs2_listxattr,
1296 .removexattr = gfs2_removexattr,
1297 .fiemap = gfs2_fiemap,
1298 };
1299