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