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