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