blob: 9fb9490eb67afe8e256a661f595aed8eebd027af [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 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 v.2.
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>
20#include <asm/semaphore.h>
21#include <asm/uaccess.h>
22
23#include "gfs2.h"
24#include "acl.h"
25#include "bmap.h"
26#include "dir.h"
27#include "eaops.h"
28#include "eattr.h"
29#include "glock.h"
30#include "inode.h"
31#include "meta_io.h"
32#include "ops_dentry.h"
33#include "ops_inode.h"
34#include "page.h"
35#include "quota.h"
36#include "rgrp.h"
37#include "trans.h"
38#include "unlinked.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
49static int gfs2_create(struct inode *dir, struct dentry *dentry,
50 int mode, struct nameidata *nd)
51{
Steven Whitehouse7359a192006-02-13 12:27:43 +000052 struct gfs2_inode *dip = get_v2ip(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +000053 struct gfs2_sbd *sdp = dip->i_sbd;
54 struct gfs2_holder ghs[2];
55 struct inode *inode;
56 int new = 1;
57 int error;
58
59 atomic_inc(&sdp->sd_ops_inode);
60
61 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
62
63 for (;;) {
Steven Whitehouse7359a192006-02-13 12:27:43 +000064 inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode);
65 if (!IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +000066 gfs2_trans_end(sdp);
67 if (dip->i_alloc.al_rgd)
68 gfs2_inplace_release(dip);
69 gfs2_quota_unlock(dip);
70 gfs2_alloc_put(dip);
71 gfs2_glock_dq_uninit_m(2, ghs);
72 break;
Steven Whitehouse7359a192006-02-13 12:27:43 +000073 } else if (PTR_ERR(inode) != -EEXIST ||
David Teiglandb3b94fa2006-01-16 16:50:04 +000074 (nd->intent.open.flags & O_EXCL)) {
75 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +000076 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000077 }
78
Steven Whitehouse7359a192006-02-13 12:27:43 +000079 error = gfs2_lookupi(dir, &dentry->d_name, 0, &inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000080 if (!error) {
81 new = 0;
82 gfs2_holder_uninit(ghs);
83 break;
84 } else if (error != -ENOENT) {
85 gfs2_holder_uninit(ghs);
86 return error;
87 }
88 }
89
David Teiglandb3b94fa2006-01-16 16:50:04 +000090 d_instantiate(dentry, inode);
91 if (new)
92 mark_inode_dirty(inode);
93
94 return 0;
95}
96
97/**
98 * gfs2_lookup - Look up a filename in a directory and return its inode
99 * @dir: The directory inode
100 * @dentry: The dentry of the new inode
101 * @nd: passed from Linux VFS, ignored by us
102 *
103 * Called by the VFS layer. Lock dir and call gfs2_lookupi()
104 *
105 * Returns: errno
106 */
107
108static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
109 struct nameidata *nd)
110{
Steven Whitehouse7359a192006-02-13 12:27:43 +0000111 struct gfs2_inode *dip = get_v2ip(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000112 struct gfs2_sbd *sdp = dip->i_sbd;
113 struct inode *inode = NULL;
114 int error;
115
116 atomic_inc(&sdp->sd_ops_inode);
117
118 if (!sdp->sd_args.ar_localcaching)
119 dentry->d_op = &gfs2_dops;
120
Steven Whitehouse7359a192006-02-13 12:27:43 +0000121 error = gfs2_lookupi(dir, &dentry->d_name, 0, &inode);
122 if (error && error != -ENOENT)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000123 return ERR_PTR(error);
124
125 if (inode)
126 return d_splice_alias(inode, dentry);
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
144static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
145 struct dentry *dentry)
146{
147 struct gfs2_inode *dip = get_v2ip(dir);
148 struct gfs2_sbd *sdp = dip->i_sbd;
149 struct inode *inode = old_dentry->d_inode;
150 struct gfs2_inode *ip = get_v2ip(inode);
151 struct gfs2_holder ghs[2];
152 int alloc_required;
153 int error;
154
155 atomic_inc(&sdp->sd_ops_inode);
156
157 if (S_ISDIR(ip->i_di.di_mode))
158 return -EPERM;
159
160 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
161 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
162
163 error = gfs2_glock_nq_m(2, ghs);
164 if (error)
165 goto out;
166
167 error = gfs2_repermission(dir, MAY_WRITE | MAY_EXEC, NULL);
168 if (error)
169 goto out_gunlock;
170
171 error = gfs2_dir_search(dip, &dentry->d_name, NULL, NULL);
172 switch (error) {
173 case -ENOENT:
174 break;
175 case 0:
176 error = -EEXIST;
177 default:
178 goto out_gunlock;
179 }
180
181 error = -EINVAL;
182 if (!dip->i_di.di_nlink)
183 goto out_gunlock;
184 error = -EFBIG;
185 if (dip->i_di.di_entries == (uint32_t)-1)
186 goto out_gunlock;
187 error = -EPERM;
188 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
189 goto out_gunlock;
190 error = -EINVAL;
191 if (!ip->i_di.di_nlink)
192 goto out_gunlock;
193 error = -EMLINK;
194 if (ip->i_di.di_nlink == (uint32_t)-1)
195 goto out_gunlock;
196
197 error = gfs2_diradd_alloc_required(dip, &dentry->d_name,
198 &alloc_required);
199 if (error)
200 goto out_gunlock;
201
202 if (alloc_required) {
203 struct gfs2_alloc *al = gfs2_alloc_get(dip);
204
205 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
206 if (error)
207 goto out_alloc;
208
209 error = gfs2_quota_check(dip, dip->i_di.di_uid,
210 dip->i_di.di_gid);
211 if (error)
212 goto out_gunlock_q;
213
214 al->al_requested = sdp->sd_max_dirres;
215
216 error = gfs2_inplace_reserve(dip);
217 if (error)
218 goto out_gunlock_q;
219
220 error = gfs2_trans_begin(sdp,
221 sdp->sd_max_dirres +
222 al->al_rgd->rd_ri.ri_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(dip, &dentry->d_name, &ip->i_num,
234 IF2DT(ip->i_di.di_mode));
235 if (error)
236 goto out_end_trans;
237
238 error = gfs2_change_nlink(ip, +1);
239
240 out_end_trans:
241 gfs2_trans_end(sdp);
242
243 out_ipres:
244 if (alloc_required)
245 gfs2_inplace_release(dip);
246
247 out_gunlock_q:
248 if (alloc_required)
249 gfs2_quota_unlock(dip);
250
251 out_alloc:
252 if (alloc_required)
253 gfs2_alloc_put(dip);
254
255 out_gunlock:
256 gfs2_glock_dq_m(2, ghs);
257
258 out:
259 gfs2_holder_uninit(ghs);
260 gfs2_holder_uninit(ghs + 1);
261
262 if (!error) {
263 atomic_inc(&inode->i_count);
264 d_instantiate(dentry, inode);
265 mark_inode_dirty(inode);
266 }
267
268 return error;
269}
270
271/**
272 * gfs2_unlink - Unlink a file
273 * @dir: The inode of the directory containing the file to unlink
274 * @dentry: The file itself
275 *
276 * Unlink a file. Call gfs2_unlinki()
277 *
278 * Returns: errno
279 */
280
281static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
282{
283 struct gfs2_inode *dip = get_v2ip(dir);
284 struct gfs2_sbd *sdp = dip->i_sbd;
285 struct gfs2_inode *ip = get_v2ip(dentry->d_inode);
286 struct gfs2_unlinked *ul;
287 struct gfs2_holder ghs[2];
288 int error;
289
290 atomic_inc(&sdp->sd_ops_inode);
291
292 error = gfs2_unlinked_get(sdp, &ul);
293 if (error)
294 return error;
295
296 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
297 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
298
299 error = gfs2_glock_nq_m(2, ghs);
300 if (error)
301 goto out;
302
303 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
304 if (error)
305 goto out_gunlock;
306
307 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF +
308 RES_UNLINKED, 0);
309 if (error)
310 goto out_gunlock;
311
312 error = gfs2_unlinki(dip, &dentry->d_name, ip,ul);
313
314 gfs2_trans_end(sdp);
315
316 out_gunlock:
317 gfs2_glock_dq_m(2, ghs);
318
319 out:
320 gfs2_holder_uninit(ghs);
321 gfs2_holder_uninit(ghs + 1);
322
323 gfs2_unlinked_put(sdp, ul);
324
325 return error;
326}
327
328/**
329 * gfs2_symlink - Create a symlink
330 * @dir: The directory to create the symlink in
331 * @dentry: The dentry to put the symlink in
332 * @symname: The thing which the link points to
333 *
334 * Returns: errno
335 */
336
337static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
338 const char *symname)
339{
340 struct gfs2_inode *dip = get_v2ip(dir), *ip;
341 struct gfs2_sbd *sdp = dip->i_sbd;
342 struct gfs2_holder ghs[2];
343 struct inode *inode;
344 struct buffer_head *dibh;
345 int size;
346 int error;
347
348 atomic_inc(&sdp->sd_ops_inode);
349
350 /* Must be stuffed with a null terminator for gfs2_follow_link() */
351 size = strlen(symname);
352 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
353 return -ENAMETOOLONG;
354
355 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
356
Steven Whitehouse7359a192006-02-13 12:27:43 +0000357 inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO);
358 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000359 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000360 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000361 }
362
363 ip = get_gl2ip(ghs[1].gh_gl);
364
365 ip->i_di.di_size = size;
366
367 error = gfs2_meta_inode_buffer(ip, &dibh);
368
369 if (!gfs2_assert_withdraw(sdp, !error)) {
370 gfs2_dinode_out(&ip->i_di, dibh->b_data);
371 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
372 size);
373 brelse(dibh);
374 }
375
376 gfs2_trans_end(sdp);
377 if (dip->i_alloc.al_rgd)
378 gfs2_inplace_release(dip);
379 gfs2_quota_unlock(dip);
380 gfs2_alloc_put(dip);
381
382 gfs2_glock_dq_uninit_m(2, ghs);
383
David Teiglandb3b94fa2006-01-16 16:50:04 +0000384 d_instantiate(dentry, inode);
385 mark_inode_dirty(inode);
386
387 return 0;
388}
389
390/**
391 * gfs2_mkdir - Make a directory
392 * @dir: The parent directory of the new one
393 * @dentry: The dentry of the new directory
394 * @mode: The mode of the new directory
395 *
396 * Returns: errno
397 */
398
399static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
400{
401 struct gfs2_inode *dip = get_v2ip(dir), *ip;
402 struct gfs2_sbd *sdp = dip->i_sbd;
403 struct gfs2_holder ghs[2];
404 struct inode *inode;
405 struct buffer_head *dibh;
406 int error;
407
408 atomic_inc(&sdp->sd_ops_inode);
409
410 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
411
Steven Whitehouse7359a192006-02-13 12:27:43 +0000412 inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode);
413 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000414 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000415 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000416 }
417
418 ip = get_gl2ip(ghs[1].gh_gl);
419
420 ip->i_di.di_nlink = 2;
421 ip->i_di.di_size = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
422 ip->i_di.di_flags |= GFS2_DIF_JDATA;
423 ip->i_di.di_payload_format = GFS2_FORMAT_DE;
424 ip->i_di.di_entries = 2;
425
426 error = gfs2_meta_inode_buffer(ip, &dibh);
427
428 if (!gfs2_assert_withdraw(sdp, !error)) {
429 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
430 struct gfs2_dirent *dent;
431
432 gfs2_dirent_alloc(ip, dibh, 1, &dent);
433
434 dent->de_inum = di->di_num; /* already GFS2 endian */
435 dent->de_hash = gfs2_disk_hash(".", 1);
436 dent->de_hash = cpu_to_be32(dent->de_hash);
437 dent->de_type = DT_DIR;
438 memcpy((char *) (dent + 1), ".", 1);
439 di->di_entries = cpu_to_be32(1);
440
441 gfs2_dirent_alloc(ip, dibh, 2, &dent);
442
443 gfs2_inum_out(&dip->i_num, (char *) &dent->de_inum);
444 dent->de_hash = gfs2_disk_hash("..", 2);
445 dent->de_hash = cpu_to_be32(dent->de_hash);
446 dent->de_type = DT_DIR;
447 memcpy((char *) (dent + 1), "..", 2);
448
449 gfs2_dinode_out(&ip->i_di, (char *)di);
450
451 brelse(dibh);
452 }
453
454 error = gfs2_change_nlink(dip, +1);
455 gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
456
457 gfs2_trans_end(sdp);
458 if (dip->i_alloc.al_rgd)
459 gfs2_inplace_release(dip);
460 gfs2_quota_unlock(dip);
461 gfs2_alloc_put(dip);
462
463 gfs2_glock_dq_uninit_m(2, ghs);
464
David Teiglandb3b94fa2006-01-16 16:50:04 +0000465 d_instantiate(dentry, inode);
466 mark_inode_dirty(inode);
467
468 return 0;
469}
470
471/**
472 * gfs2_rmdir - Remove a directory
473 * @dir: The parent directory of the directory to be removed
474 * @dentry: The dentry of the directory to remove
475 *
476 * Remove a directory. Call gfs2_rmdiri()
477 *
478 * Returns: errno
479 */
480
481static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
482{
483 struct gfs2_inode *dip = get_v2ip(dir);
484 struct gfs2_sbd *sdp = dip->i_sbd;
485 struct gfs2_inode *ip = get_v2ip(dentry->d_inode);
486 struct gfs2_unlinked *ul;
487 struct gfs2_holder ghs[2];
488 int error;
489
490 atomic_inc(&sdp->sd_ops_inode);
491
492 error = gfs2_unlinked_get(sdp, &ul);
493 if (error)
494 return error;
495
496 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
497 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
498
499 error = gfs2_glock_nq_m(2, ghs);
500 if (error)
501 goto out;
502
503 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
504 if (error)
505 goto out_gunlock;
506
507 if (ip->i_di.di_entries < 2) {
508 if (gfs2_consist_inode(ip))
509 gfs2_dinode_print(&ip->i_di);
510 error = -EIO;
511 goto out_gunlock;
512 }
513 if (ip->i_di.di_entries > 2) {
514 error = -ENOTEMPTY;
515 goto out_gunlock;
516 }
517
518 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF +
519 RES_UNLINKED, 0);
520 if (error)
521 goto out_gunlock;
522
523 error = gfs2_rmdiri(dip, &dentry->d_name, ip, ul);
524
525 gfs2_trans_end(sdp);
526
527 out_gunlock:
528 gfs2_glock_dq_m(2, ghs);
529
530 out:
531 gfs2_holder_uninit(ghs);
532 gfs2_holder_uninit(ghs + 1);
533
534 gfs2_unlinked_put(sdp, ul);
535
536 return error;
537}
538
539/**
540 * gfs2_mknod - Make a special file
541 * @dir: The directory in which the special file will reside
542 * @dentry: The dentry of the special file
543 * @mode: The mode of the special file
544 * @rdev: The device specification of the special file
545 *
546 */
547
548static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
549 dev_t dev)
550{
551 struct gfs2_inode *dip = get_v2ip(dir), *ip;
552 struct gfs2_sbd *sdp = dip->i_sbd;
553 struct gfs2_holder ghs[2];
554 struct inode *inode;
555 struct buffer_head *dibh;
556 uint32_t major = 0, minor = 0;
557 int error;
558
559 atomic_inc(&sdp->sd_ops_inode);
560
561 switch (mode & S_IFMT) {
562 case S_IFBLK:
563 case S_IFCHR:
564 major = MAJOR(dev);
565 minor = MINOR(dev);
566 break;
567 case S_IFIFO:
568 case S_IFSOCK:
569 break;
570 default:
571 return -EOPNOTSUPP;
572 };
573
574 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
575
Steven Whitehouse7359a192006-02-13 12:27:43 +0000576 inode = gfs2_createi(ghs, &dentry->d_name, mode);
577 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000578 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000579 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000580 }
581
582 ip = get_gl2ip(ghs[1].gh_gl);
583
584 ip->i_di.di_major = major;
585 ip->i_di.di_minor = minor;
586
587 error = gfs2_meta_inode_buffer(ip, &dibh);
588
589 if (!gfs2_assert_withdraw(sdp, !error)) {
590 gfs2_dinode_out(&ip->i_di, dibh->b_data);
591 brelse(dibh);
592 }
593
594 gfs2_trans_end(sdp);
595 if (dip->i_alloc.al_rgd)
596 gfs2_inplace_release(dip);
597 gfs2_quota_unlock(dip);
598 gfs2_alloc_put(dip);
599
600 gfs2_glock_dq_uninit_m(2, ghs);
601
David Teiglandb3b94fa2006-01-16 16:50:04 +0000602 d_instantiate(dentry, inode);
603 mark_inode_dirty(inode);
604
605 return 0;
606}
607
608/**
609 * gfs2_rename - Rename a file
610 * @odir: Parent directory of old file name
611 * @odentry: The old dentry of the file
612 * @ndir: Parent directory of new file name
613 * @ndentry: The new dentry of the file
614 *
615 * Returns: errno
616 */
617
618static int gfs2_rename(struct inode *odir, struct dentry *odentry,
619 struct inode *ndir, struct dentry *ndentry)
620{
621 struct gfs2_inode *odip = get_v2ip(odir);
622 struct gfs2_inode *ndip = get_v2ip(ndir);
623 struct gfs2_inode *ip = get_v2ip(odentry->d_inode);
624 struct gfs2_inode *nip = NULL;
625 struct gfs2_sbd *sdp = odip->i_sbd;
626 struct gfs2_unlinked *ul;
627 struct gfs2_holder ghs[4], r_gh;
628 unsigned int num_gh;
629 int dir_rename = 0;
630 int alloc_required;
631 unsigned int x;
632 int error;
633
634 atomic_inc(&sdp->sd_ops_inode);
635
636 if (ndentry->d_inode) {
637 nip = get_v2ip(ndentry->d_inode);
638 if (ip == nip)
639 return 0;
640 }
641
642 error = gfs2_unlinked_get(sdp, &ul);
643 if (error)
644 return error;
645
646 /* Make sure we aren't trying to move a dirctory into it's subdir */
647
648 if (S_ISDIR(ip->i_di.di_mode) && odip != ndip) {
649 dir_rename = 1;
650
651 error = gfs2_glock_nq_init(sdp->sd_rename_gl,
652 LM_ST_EXCLUSIVE, 0,
653 &r_gh);
654 if (error)
655 goto out;
656
657 error = gfs2_ok_to_move(ip, ndip);
658 if (error)
659 goto out_gunlock_r;
660 }
661
662 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
663 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
664 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
665 num_gh = 3;
666
667 if (nip)
668 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
669
670 error = gfs2_glock_nq_m(num_gh, ghs);
671 if (error)
672 goto out_uninit;
673
674 /* Check out the old directory */
675
676 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
677 if (error)
678 goto out_gunlock;
679
680 /* Check out the new directory */
681
682 if (nip) {
683 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
684 if (error)
685 goto out_gunlock;
686
687 if (S_ISDIR(nip->i_di.di_mode)) {
688 if (nip->i_di.di_entries < 2) {
689 if (gfs2_consist_inode(nip))
690 gfs2_dinode_print(&nip->i_di);
691 error = -EIO;
692 goto out_gunlock;
693 }
694 if (nip->i_di.di_entries > 2) {
695 error = -ENOTEMPTY;
696 goto out_gunlock;
697 }
698 }
699 } else {
700 error = gfs2_repermission(ndir, MAY_WRITE | MAY_EXEC, NULL);
701 if (error)
702 goto out_gunlock;
703
704 error = gfs2_dir_search(ndip, &ndentry->d_name, NULL, NULL);
705 switch (error) {
706 case -ENOENT:
707 error = 0;
708 break;
709 case 0:
710 error = -EEXIST;
711 default:
712 goto out_gunlock;
713 };
714
715 if (odip != ndip) {
716 if (!ndip->i_di.di_nlink) {
717 error = -EINVAL;
718 goto out_gunlock;
719 }
720 if (ndip->i_di.di_entries == (uint32_t)-1) {
721 error = -EFBIG;
722 goto out_gunlock;
723 }
724 if (S_ISDIR(ip->i_di.di_mode) &&
725 ndip->i_di.di_nlink == (uint32_t)-1) {
726 error = -EMLINK;
727 goto out_gunlock;
728 }
729 }
730 }
731
732 /* Check out the dir to be renamed */
733
734 if (dir_rename) {
735 error = gfs2_repermission(odentry->d_inode, MAY_WRITE, NULL);
736 if (error)
737 goto out_gunlock;
738 }
739
740 error = gfs2_diradd_alloc_required(ndip, &ndentry->d_name,
741 &alloc_required);
742 if (error)
743 goto out_gunlock;
744
745 if (alloc_required) {
746 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
747
748 error = gfs2_quota_lock(ndip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
749 if (error)
750 goto out_alloc;
751
752 error = gfs2_quota_check(ndip, ndip->i_di.di_uid,
753 ndip->i_di.di_gid);
754 if (error)
755 goto out_gunlock_q;
756
757 al->al_requested = sdp->sd_max_dirres;
758
759 error = gfs2_inplace_reserve(ndip);
760 if (error)
761 goto out_gunlock_q;
762
763 error = gfs2_trans_begin(sdp,
764 sdp->sd_max_dirres +
765 al->al_rgd->rd_ri.ri_length +
766 4 * RES_DINODE + 4 * RES_LEAF +
767 RES_UNLINKED + RES_STATFS +
768 RES_QUOTA, 0);
769 if (error)
770 goto out_ipreserv;
771 } else {
772 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
773 5 * RES_LEAF +
774 RES_UNLINKED, 0);
775 if (error)
776 goto out_gunlock;
777 }
778
779 /* Remove the target file, if it exists */
780
781 if (nip) {
782 if (S_ISDIR(nip->i_di.di_mode))
783 error = gfs2_rmdiri(ndip, &ndentry->d_name, nip, ul);
784 else
785 error = gfs2_unlinki(ndip, &ndentry->d_name, nip, ul);
786 if (error)
787 goto out_end_trans;
788 }
789
790 if (dir_rename) {
791 struct qstr name;
792 name.len = 2;
793 name.name = "..";
794
795 error = gfs2_change_nlink(ndip, +1);
796 if (error)
797 goto out_end_trans;
798 error = gfs2_change_nlink(odip, -1);
799 if (error)
800 goto out_end_trans;
801
802 error = gfs2_dir_mvino(ip, &name, &ndip->i_num, DT_DIR);
803 if (error)
804 goto out_end_trans;
805 } else {
806 struct buffer_head *dibh;
807 error = gfs2_meta_inode_buffer(ip, &dibh);
808 if (error)
809 goto out_end_trans;
810 ip->i_di.di_ctime = get_seconds();
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000811 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000812 gfs2_dinode_out(&ip->i_di, dibh->b_data);
813 brelse(dibh);
814 }
815
816 error = gfs2_dir_del(odip, &odentry->d_name);
817 if (error)
818 goto out_end_trans;
819
820 error = gfs2_dir_add(ndip, &ndentry->d_name, &ip->i_num,
821 IF2DT(ip->i_di.di_mode));
822 if (error)
823 goto out_end_trans;
824
825 out_end_trans:
826 gfs2_trans_end(sdp);
827
828 out_ipreserv:
829 if (alloc_required)
830 gfs2_inplace_release(ndip);
831
832 out_gunlock_q:
833 if (alloc_required)
834 gfs2_quota_unlock(ndip);
835
836 out_alloc:
837 if (alloc_required)
838 gfs2_alloc_put(ndip);
839
840 out_gunlock:
841 gfs2_glock_dq_m(num_gh, ghs);
842
843 out_uninit:
844 for (x = 0; x < num_gh; x++)
845 gfs2_holder_uninit(ghs + x);
846
847 out_gunlock_r:
848 if (dir_rename)
849 gfs2_glock_dq_uninit(&r_gh);
850
851 out:
852 gfs2_unlinked_put(sdp, ul);
853
854 return error;
855}
856
857/**
858 * gfs2_readlink - Read the value of a symlink
859 * @dentry: the symlink
860 * @buf: the buffer to read the symlink data into
861 * @size: the size of the buffer
862 *
863 * Returns: errno
864 */
865
866static int gfs2_readlink(struct dentry *dentry, char __user *user_buf,
867 int user_size)
868{
869 struct gfs2_inode *ip = get_v2ip(dentry->d_inode);
870 char array[GFS2_FAST_NAME_SIZE], *buf = array;
871 unsigned int len = GFS2_FAST_NAME_SIZE;
872 int error;
873
874 atomic_inc(&ip->i_sbd->sd_ops_inode);
875
876 error = gfs2_readlinki(ip, &buf, &len);
877 if (error)
878 return error;
879
880 if (user_size > len - 1)
881 user_size = len - 1;
882
883 if (copy_to_user(user_buf, buf, user_size))
884 error = -EFAULT;
885 else
886 error = user_size;
887
888 if (buf != array)
889 kfree(buf);
890
891 return error;
892}
893
894/**
895 * gfs2_follow_link - Follow a symbolic link
896 * @dentry: The dentry of the link
897 * @nd: Data that we pass to vfs_follow_link()
898 *
899 * This can handle symlinks of any size. It is optimised for symlinks
900 * under GFS2_FAST_NAME_SIZE.
901 *
902 * Returns: 0 on success or error code
903 */
904
905static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
906{
907 struct gfs2_inode *ip = get_v2ip(dentry->d_inode);
908 char array[GFS2_FAST_NAME_SIZE], *buf = array;
909 unsigned int len = GFS2_FAST_NAME_SIZE;
910 int error;
911
912 atomic_inc(&ip->i_sbd->sd_ops_inode);
913
914 error = gfs2_readlinki(ip, &buf, &len);
915 if (!error) {
916 error = vfs_follow_link(nd, buf);
917 if (buf != array)
918 kfree(buf);
919 }
920
921 return ERR_PTR(error);
922}
923
924/**
925 * gfs2_permission -
926 * @inode:
927 * @mask:
928 * @nd: passed from Linux VFS, ignored by us
929 *
930 * Returns: errno
931 */
932
933static int gfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
934{
935 struct gfs2_inode *ip = get_v2ip(inode);
936 struct gfs2_holder i_gh;
937 int error;
938
939 atomic_inc(&ip->i_sbd->sd_ops_inode);
940
941 if (ip->i_vn == ip->i_gl->gl_vn)
942 return generic_permission(inode, mask, gfs2_check_acl);
943
944 error = gfs2_glock_nq_init(ip->i_gl,
945 LM_ST_SHARED, LM_FLAG_ANY,
946 &i_gh);
947 if (!error) {
948 error = generic_permission(inode, mask, gfs2_check_acl_locked);
949 gfs2_glock_dq_uninit(&i_gh);
950 }
951
952 return error;
953}
954
955static int setattr_size(struct inode *inode, struct iattr *attr)
956{
957 struct gfs2_inode *ip = get_v2ip(inode);
958 int error;
959
960 if (attr->ia_size != ip->i_di.di_size) {
961 error = vmtruncate(inode, attr->ia_size);
962 if (error)
963 return error;
964 }
965
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +0000966 error = gfs2_truncatei(ip, attr->ia_size);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000967 if (error)
968 return error;
969
970 return error;
971}
972
973static int setattr_chown(struct inode *inode, struct iattr *attr)
974{
975 struct gfs2_inode *ip = get_v2ip(inode);
976 struct gfs2_sbd *sdp = ip->i_sbd;
977 struct buffer_head *dibh;
978 uint32_t ouid, ogid, nuid, ngid;
979 int error;
980
981 ouid = ip->i_di.di_uid;
982 ogid = ip->i_di.di_gid;
983 nuid = attr->ia_uid;
984 ngid = attr->ia_gid;
985
986 if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
987 ouid = nuid = NO_QUOTA_CHANGE;
988 if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
989 ogid = ngid = NO_QUOTA_CHANGE;
990
991 gfs2_alloc_get(ip);
992
993 error = gfs2_quota_lock(ip, nuid, ngid);
994 if (error)
995 goto out_alloc;
996
997 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
998 error = gfs2_quota_check(ip, nuid, ngid);
999 if (error)
1000 goto out_gunlock_q;
1001 }
1002
1003 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
1004 if (error)
1005 goto out_gunlock_q;
1006
1007 error = gfs2_meta_inode_buffer(ip, &dibh);
1008 if (error)
1009 goto out_end_trans;
1010
1011 error = inode_setattr(inode, attr);
1012 gfs2_assert_warn(sdp, !error);
1013 gfs2_inode_attr_out(ip);
1014
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001015 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001016 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1017 brelse(dibh);
1018
1019 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1020 gfs2_quota_change(ip, -ip->i_di.di_blocks,
1021 ouid, ogid);
1022 gfs2_quota_change(ip, ip->i_di.di_blocks,
1023 nuid, ngid);
1024 }
1025
1026 out_end_trans:
1027 gfs2_trans_end(sdp);
1028
1029 out_gunlock_q:
1030 gfs2_quota_unlock(ip);
1031
1032 out_alloc:
1033 gfs2_alloc_put(ip);
1034
1035 return error;
1036}
1037
1038/**
1039 * gfs2_setattr - Change attributes on an inode
1040 * @dentry: The dentry which is changing
1041 * @attr: The structure describing the change
1042 *
1043 * The VFS layer wants to change one or more of an inodes attributes. Write
1044 * that change out to disk.
1045 *
1046 * Returns: errno
1047 */
1048
1049static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
1050{
1051 struct inode *inode = dentry->d_inode;
1052 struct gfs2_inode *ip = get_v2ip(inode);
1053 struct gfs2_holder i_gh;
1054 int error;
1055
1056 atomic_inc(&ip->i_sbd->sd_ops_inode);
1057
1058 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1059 if (error)
1060 return error;
1061
1062 error = -EPERM;
1063 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1064 goto out;
1065
1066 error = inode_change_ok(inode, attr);
1067 if (error)
1068 goto out;
1069
1070 if (attr->ia_valid & ATTR_SIZE)
1071 error = setattr_size(inode, attr);
1072 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1073 error = setattr_chown(inode, attr);
1074 else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1075 error = gfs2_acl_chmod(ip, attr);
1076 else
1077 error = gfs2_setattr_simple(ip, attr);
1078
1079 out:
1080 gfs2_glock_dq_uninit(&i_gh);
1081
1082 if (!error)
1083 mark_inode_dirty(inode);
1084
1085 return error;
1086}
1087
1088/**
1089 * gfs2_getattr - Read out an inode's attributes
1090 * @mnt: ?
1091 * @dentry: The dentry to stat
1092 * @stat: The inode's stats
1093 *
1094 * Returns: errno
1095 */
1096
1097static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1098 struct kstat *stat)
1099{
1100 struct inode *inode = dentry->d_inode;
1101 struct gfs2_inode *ip = get_v2ip(inode);
1102 struct gfs2_holder gh;
1103 int error;
1104
1105 atomic_inc(&ip->i_sbd->sd_ops_inode);
1106
1107 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1108 if (!error) {
1109 generic_fillattr(inode, stat);
1110 gfs2_glock_dq_uninit(&gh);
1111 }
1112
1113 return error;
1114}
1115
1116static int gfs2_setxattr(struct dentry *dentry, const char *name,
1117 const void *data, size_t size, int flags)
1118{
1119 struct gfs2_inode *ip = get_v2ip(dentry->d_inode);
1120 struct gfs2_ea_request er;
1121
1122 atomic_inc(&ip->i_sbd->sd_ops_inode);
1123
1124 memset(&er, 0, sizeof(struct gfs2_ea_request));
1125 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1126 if (er.er_type == GFS2_EATYPE_UNUSED)
1127 return -EOPNOTSUPP;
1128 er.er_data = (char *)data;
1129 er.er_name_len = strlen(er.er_name);
1130 er.er_data_len = size;
1131 er.er_flags = flags;
1132
1133 gfs2_assert_warn(ip->i_sbd, !(er.er_flags & GFS2_ERF_MODE));
1134
1135 return gfs2_ea_set(ip, &er);
1136}
1137
1138static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1139 void *data, size_t size)
1140{
1141 struct gfs2_ea_request er;
1142
1143 atomic_inc(&get_v2sdp(dentry->d_inode->i_sb)->sd_ops_inode);
1144
1145 memset(&er, 0, sizeof(struct gfs2_ea_request));
1146 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1147 if (er.er_type == GFS2_EATYPE_UNUSED)
1148 return -EOPNOTSUPP;
1149 er.er_data = data;
1150 er.er_name_len = strlen(er.er_name);
1151 er.er_data_len = size;
1152
1153 return gfs2_ea_get(get_v2ip(dentry->d_inode), &er);
1154}
1155
1156static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
1157{
1158 struct gfs2_ea_request er;
1159
1160 atomic_inc(&get_v2sdp(dentry->d_inode->i_sb)->sd_ops_inode);
1161
1162 memset(&er, 0, sizeof(struct gfs2_ea_request));
1163 er.er_data = (size) ? buffer : NULL;
1164 er.er_data_len = size;
1165
1166 return gfs2_ea_list(get_v2ip(dentry->d_inode), &er);
1167}
1168
1169static int gfs2_removexattr(struct dentry *dentry, const char *name)
1170{
1171 struct gfs2_ea_request er;
1172
1173 atomic_inc(&get_v2sdp(dentry->d_inode->i_sb)->sd_ops_inode);
1174
1175 memset(&er, 0, sizeof(struct gfs2_ea_request));
1176 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1177 if (er.er_type == GFS2_EATYPE_UNUSED)
1178 return -EOPNOTSUPP;
1179 er.er_name_len = strlen(er.er_name);
1180
1181 return gfs2_ea_remove(get_v2ip(dentry->d_inode), &er);
1182}
1183
1184struct inode_operations gfs2_file_iops = {
1185 .permission = gfs2_permission,
1186 .setattr = gfs2_setattr,
1187 .getattr = gfs2_getattr,
1188 .setxattr = gfs2_setxattr,
1189 .getxattr = gfs2_getxattr,
1190 .listxattr = gfs2_listxattr,
1191 .removexattr = gfs2_removexattr,
1192};
1193
1194struct inode_operations gfs2_dev_iops = {
1195 .permission = gfs2_permission,
1196 .setattr = gfs2_setattr,
1197 .getattr = gfs2_getattr,
1198 .setxattr = gfs2_setxattr,
1199 .getxattr = gfs2_getxattr,
1200 .listxattr = gfs2_listxattr,
1201 .removexattr = gfs2_removexattr,
1202};
1203
1204struct inode_operations gfs2_dir_iops = {
1205 .create = gfs2_create,
1206 .lookup = gfs2_lookup,
1207 .link = gfs2_link,
1208 .unlink = gfs2_unlink,
1209 .symlink = gfs2_symlink,
1210 .mkdir = gfs2_mkdir,
1211 .rmdir = gfs2_rmdir,
1212 .mknod = gfs2_mknod,
1213 .rename = gfs2_rename,
1214 .permission = gfs2_permission,
1215 .setattr = gfs2_setattr,
1216 .getattr = gfs2_getattr,
1217 .setxattr = gfs2_setxattr,
1218 .getxattr = gfs2_getxattr,
1219 .listxattr = gfs2_listxattr,
1220 .removexattr = gfs2_removexattr,
1221};
1222
1223struct inode_operations gfs2_symlink_iops = {
1224 .readlink = gfs2_readlink,
1225 .follow_link = gfs2_follow_link,
1226 .permission = gfs2_permission,
1227 .setattr = gfs2_setattr,
1228 .getattr = gfs2_getattr,
1229 .setxattr = gfs2_setxattr,
1230 .getxattr = gfs2_getxattr,
1231 .listxattr = gfs2_listxattr,
1232 .removexattr = gfs2_removexattr,
1233};
1234