blob: 932797ba433b0b5d2ac1a4c06f122564990049d0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) International Business Machines Corp., 2000-2004
3 * Portions Copyright (C) Christoph Hellwig, 2001-2002
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
Dave Kleikamp63f83c92006-10-02 09:55:27 -05007 * the Free Software Foundation; either version 2 of the License, or
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * (at your option) any later version.
Dave Kleikamp63f83c92006-10-02 09:55:27 -05009 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
Dave Kleikamp63f83c92006-10-02 09:55:27 -050016 * along with this program; if not, write to the Free Software
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/fs.h>
21#include <linux/ctype.h>
22#include <linux/quotaops.h>
23#include "jfs_incore.h"
24#include "jfs_superblock.h"
25#include "jfs_inode.h"
26#include "jfs_dinode.h"
27#include "jfs_dmap.h"
28#include "jfs_unicode.h"
29#include "jfs_metapage.h"
30#include "jfs_xattr.h"
31#include "jfs_acl.h"
32#include "jfs_debug.h"
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/*
35 * forward references
36 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070037struct dentry_operations jfs_ci_dentry_operations;
38
39static s64 commitZeroLink(tid_t, struct inode *);
40
41/*
Dave Kleikamp4f4b4012005-09-01 09:02:43 -050042 * NAME: free_ea_wmap(inode)
43 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -050044 * FUNCTION: free uncommitted extended attributes from working map
Dave Kleikamp4f4b4012005-09-01 09:02:43 -050045 *
46 */
47static inline void free_ea_wmap(struct inode *inode)
48{
49 dxd_t *ea = &JFS_IP(inode)->ea;
50
51 if (ea->flag & DXD_EXTENT) {
52 /* free EA pages from cache */
53 invalidate_dxd_metapages(inode, *ea);
54 dbFree(inode, addressDXD(ea), lengthDXD(ea));
55 }
56 ea->flag = 0;
57}
58
59/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 * NAME: jfs_create(dip, dentry, mode)
61 *
62 * FUNCTION: create a regular file in the parent directory <dip>
63 * with name = <from dentry> and mode = <mode>
64 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -050065 * PARAMETER: dip - parent directory vnode
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 * dentry - dentry of new file
67 * mode - create mode (rwxrwxrwx).
68 * nd- nd struct
69 *
70 * RETURN: Errors from subroutines
71 *
72 */
73static int jfs_create(struct inode *dip, struct dentry *dentry, int mode,
74 struct nameidata *nd)
75{
76 int rc = 0;
77 tid_t tid; /* transaction id */
78 struct inode *ip = NULL; /* child directory inode */
79 ino_t ino;
80 struct component_name dname; /* child directory name */
81 struct btstack btstack;
82 struct inode *iplist[2];
83 struct tblock *tblk;
84
85 jfs_info("jfs_create: dip:0x%p name:%s", dip, dentry->d_name.name);
86
87 /*
88 * search parent directory for entry/freespace
89 * (dtSearch() returns parent directory page pinned)
90 */
91 if ((rc = get_UCSname(&dname, dentry)))
92 goto out1;
93
94 /*
95 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
96 * block there while holding dtree page, so we allocate the inode &
97 * begin the transaction before we search the directory.
98 */
99 ip = ialloc(dip, mode);
Akinobu Mita087387f2006-09-14 09:22:38 -0500100 if (IS_ERR(ip)) {
101 rc = PTR_ERR(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 goto out2;
103 }
104
105 tid = txBegin(dip->i_sb, 0);
106
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600107 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
108 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500110 rc = jfs_init_acl(tid, ip, dip);
111 if (rc)
112 goto out3;
113
Dave Kleikamp1d15b10f2005-09-01 09:05:39 -0500114 rc = jfs_init_security(tid, ip, dip);
115 if (rc) {
116 txAbort(tid, 0);
117 goto out3;
118 }
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
121 jfs_err("jfs_create: dtSearch returned %d", rc);
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500122 txAbort(tid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 goto out3;
124 }
125
126 tblk = tid_to_tblock(tid);
127 tblk->xflag |= COMMIT_CREATE;
128 tblk->ino = ip->i_ino;
129 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
130
131 iplist[0] = dip;
132 iplist[1] = ip;
133
134 /*
135 * initialize the child XAD tree root in-line in inode
136 */
137 xtInitRoot(tid, ip);
138
139 /*
140 * create entry in parent directory for child directory
141 * (dtInsert() releases parent directory page)
142 */
143 ino = ip->i_ino;
144 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
145 if (rc == -EIO) {
146 jfs_err("jfs_create: dtInsert returned -EIO");
147 txAbort(tid, 1); /* Marks Filesystem dirty */
148 } else
149 txAbort(tid, 0); /* Filesystem full */
150 goto out3;
151 }
152
153 ip->i_op = &jfs_file_inode_operations;
154 ip->i_fop = &jfs_file_operations;
155 ip->i_mapping->a_ops = &jfs_aops;
156
157 insert_inode_hash(ip);
158 mark_inode_dirty(ip);
159
160 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
161
162 mark_inode_dirty(dip);
163
164 rc = txCommit(tid, 2, &iplist[0], 0);
165
166 out3:
167 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600168 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500169 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (rc) {
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500171 free_ea_wmap(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 ip->i_nlink = 0;
173 iput(ip);
174 } else
175 d_instantiate(dentry, ip);
176
177 out2:
178 free_UCSname(&dname);
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 out1:
181
182 jfs_info("jfs_create: rc:%d", rc);
183 return rc;
184}
185
186
187/*
188 * NAME: jfs_mkdir(dip, dentry, mode)
189 *
190 * FUNCTION: create a child directory in the parent directory <dip>
191 * with name = <from dentry> and mode = <mode>
192 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500193 * PARAMETER: dip - parent directory vnode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 * dentry - dentry of child directory
195 * mode - create mode (rwxrwxrwx).
196 *
197 * RETURN: Errors from subroutines
198 *
199 * note:
200 * EACCESS: user needs search+write permission on the parent directory
201 */
202static int jfs_mkdir(struct inode *dip, struct dentry *dentry, int mode)
203{
204 int rc = 0;
205 tid_t tid; /* transaction id */
206 struct inode *ip = NULL; /* child directory inode */
207 ino_t ino;
208 struct component_name dname; /* child directory name */
209 struct btstack btstack;
210 struct inode *iplist[2];
211 struct tblock *tblk;
212
213 jfs_info("jfs_mkdir: dip:0x%p name:%s", dip, dentry->d_name.name);
214
215 /* link count overflow on parent directory ? */
216 if (dip->i_nlink == JFS_LINK_MAX) {
217 rc = -EMLINK;
218 goto out1;
219 }
220
221 /*
222 * search parent directory for entry/freespace
223 * (dtSearch() returns parent directory page pinned)
224 */
225 if ((rc = get_UCSname(&dname, dentry)))
226 goto out1;
227
228 /*
229 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
230 * block there while holding dtree page, so we allocate the inode &
231 * begin the transaction before we search the directory.
232 */
233 ip = ialloc(dip, S_IFDIR | mode);
Akinobu Mita087387f2006-09-14 09:22:38 -0500234 if (IS_ERR(ip)) {
235 rc = PTR_ERR(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 goto out2;
237 }
238
239 tid = txBegin(dip->i_sb, 0);
240
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600241 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
242 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500244 rc = jfs_init_acl(tid, ip, dip);
245 if (rc)
246 goto out3;
247
Dave Kleikamp1d15b10f2005-09-01 09:05:39 -0500248 rc = jfs_init_security(tid, ip, dip);
249 if (rc) {
250 txAbort(tid, 0);
251 goto out3;
252 }
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
255 jfs_err("jfs_mkdir: dtSearch returned %d", rc);
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500256 txAbort(tid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 goto out3;
258 }
259
260 tblk = tid_to_tblock(tid);
261 tblk->xflag |= COMMIT_CREATE;
262 tblk->ino = ip->i_ino;
263 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
264
265 iplist[0] = dip;
266 iplist[1] = ip;
267
268 /*
269 * initialize the child directory in-line in inode
270 */
271 dtInitRoot(tid, ip, dip->i_ino);
272
273 /*
274 * create entry in parent directory for child directory
275 * (dtInsert() releases parent directory page)
276 */
277 ino = ip->i_ino;
278 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
279 if (rc == -EIO) {
280 jfs_err("jfs_mkdir: dtInsert returned -EIO");
281 txAbort(tid, 1); /* Marks Filesystem dirty */
282 } else
283 txAbort(tid, 0); /* Filesystem full */
284 goto out3;
285 }
286
287 ip->i_nlink = 2; /* for '.' */
288 ip->i_op = &jfs_dir_inode_operations;
289 ip->i_fop = &jfs_dir_operations;
290
291 insert_inode_hash(ip);
292 mark_inode_dirty(ip);
293
294 /* update parent directory inode */
Dave Hansend8c76e62006-09-30 23:29:04 -0700295 inc_nlink(dip); /* for '..' from child directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
297 mark_inode_dirty(dip);
298
299 rc = txCommit(tid, 2, &iplist[0], 0);
300
301 out3:
302 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600303 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500304 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 if (rc) {
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500306 free_ea_wmap(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 ip->i_nlink = 0;
308 iput(ip);
309 } else
310 d_instantiate(dentry, ip);
311
312 out2:
313 free_UCSname(&dname);
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 out1:
317
318 jfs_info("jfs_mkdir: rc:%d", rc);
319 return rc;
320}
321
322/*
323 * NAME: jfs_rmdir(dip, dentry)
324 *
325 * FUNCTION: remove a link to child directory
326 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500327 * PARAMETER: dip - parent inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 * dentry - child directory dentry
329 *
330 * RETURN: -EINVAL - if name is . or ..
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500331 * -EINVAL - if . or .. exist but are invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 * errors from subroutines
333 *
334 * note:
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500335 * if other threads have the directory open when the last link
336 * is removed, the "." and ".." entries, if present, are removed before
337 * rmdir() returns and no new entries may be created in the directory,
338 * but the directory is not removed until the last reference to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 * the directory is released (cf.unlink() of regular file).
340 */
341static int jfs_rmdir(struct inode *dip, struct dentry *dentry)
342{
343 int rc;
344 tid_t tid; /* transaction id */
345 struct inode *ip = dentry->d_inode;
346 ino_t ino;
347 struct component_name dname;
348 struct inode *iplist[2];
349 struct tblock *tblk;
350
351 jfs_info("jfs_rmdir: dip:0x%p name:%s", dip, dentry->d_name.name);
352
353 /* Init inode for quota operations. */
354 DQUOT_INIT(ip);
355
356 /* directory must be empty to be removed */
357 if (!dtEmpty(ip)) {
358 rc = -ENOTEMPTY;
359 goto out;
360 }
361
362 if ((rc = get_UCSname(&dname, dentry))) {
363 goto out;
364 }
365
366 tid = txBegin(dip->i_sb, 0);
367
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600368 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
369 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 iplist[0] = dip;
372 iplist[1] = ip;
373
374 tblk = tid_to_tblock(tid);
375 tblk->xflag |= COMMIT_DELETE;
376 tblk->u.ip = ip;
377
378 /*
379 * delete the entry of target directory from parent directory
380 */
381 ino = ip->i_ino;
382 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
383 jfs_err("jfs_rmdir: dtDelete returned %d", rc);
384 if (rc == -EIO)
385 txAbort(tid, 1);
386 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600387 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500388 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 goto out2;
391 }
392
393 /* update parent directory's link count corresponding
394 * to ".." entry of the target directory deleted
395 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700397 inode_dec_link_count(dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 /*
400 * OS/2 could have created EA and/or ACL
401 */
402 /* free EA from both persistent and working map */
403 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
404 /* free EA pages */
405 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
406 }
407 JFS_IP(ip)->ea.flag = 0;
408
409 /* free ACL from both persistent and working map */
410 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
411 /* free ACL pages */
412 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
413 }
414 JFS_IP(ip)->acl.flag = 0;
415
416 /* mark the target directory as deleted */
Dave Hansence71ec32006-09-30 23:29:06 -0700417 clear_nlink(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 mark_inode_dirty(ip);
419
420 rc = txCommit(tid, 2, &iplist[0], 0);
421
422 txEnd(tid);
423
Ingo Molnar1de87442006-01-24 15:22:50 -0600424 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500425 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 /*
428 * Truncating the directory index table is not guaranteed. It
429 * may need to be done iteratively
430 */
431 if (test_cflag(COMMIT_Stale, dip)) {
432 if (dip->i_size > 1)
433 jfs_truncate_nolock(dip, 0);
434
435 clear_cflag(COMMIT_Stale, dip);
436 }
437
438 out2:
439 free_UCSname(&dname);
440
441 out:
442 jfs_info("jfs_rmdir: rc:%d", rc);
443 return rc;
444}
445
446/*
447 * NAME: jfs_unlink(dip, dentry)
448 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500449 * FUNCTION: remove a link to object <vp> named by <name>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 * from parent directory <dvp>
451 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500452 * PARAMETER: dip - inode of parent directory
453 * dentry - dentry of object to be removed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 *
455 * RETURN: errors from subroutines
456 *
457 * note:
458 * temporary file: if one or more processes have the file open
459 * when the last link is removed, the link will be removed before
460 * unlink() returns, but the removal of the file contents will be
461 * postponed until all references to the files are closed.
462 *
463 * JFS does NOT support unlink() on directories.
464 *
465 */
466static int jfs_unlink(struct inode *dip, struct dentry *dentry)
467{
468 int rc;
469 tid_t tid; /* transaction id */
470 struct inode *ip = dentry->d_inode;
471 ino_t ino;
472 struct component_name dname; /* object name */
473 struct inode *iplist[2];
474 struct tblock *tblk;
475 s64 new_size = 0;
476 int commit_flag;
477
478 jfs_info("jfs_unlink: dip:0x%p name:%s", dip, dentry->d_name.name);
479
480 /* Init inode for quota operations. */
481 DQUOT_INIT(ip);
482
483 if ((rc = get_UCSname(&dname, dentry)))
484 goto out;
485
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600486 IWRITE_LOCK(ip, RDWRLOCK_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 tid = txBegin(dip->i_sb, 0);
489
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600490 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
491 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 iplist[0] = dip;
494 iplist[1] = ip;
495
496 /*
497 * delete the entry of target file from parent directory
498 */
499 ino = ip->i_ino;
500 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
501 jfs_err("jfs_unlink: dtDelete returned %d", rc);
502 if (rc == -EIO)
503 txAbort(tid, 1); /* Marks FS Dirty */
504 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600505 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500506 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 IWRITE_UNLOCK(ip);
508 goto out1;
509 }
510
511 ASSERT(ip->i_nlink);
512
513 ip->i_ctime = dip->i_ctime = dip->i_mtime = CURRENT_TIME;
514 mark_inode_dirty(dip);
515
516 /* update target's inode */
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700517 inode_dec_link_count(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 /*
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500520 * commit zero link count object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 */
522 if (ip->i_nlink == 0) {
523 assert(!test_cflag(COMMIT_Nolink, ip));
524 /* free block resources */
525 if ((new_size = commitZeroLink(tid, ip)) < 0) {
526 txAbort(tid, 1); /* Marks FS Dirty */
527 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600528 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500529 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 IWRITE_UNLOCK(ip);
531 rc = new_size;
532 goto out1;
533 }
534 tblk = tid_to_tblock(tid);
535 tblk->xflag |= COMMIT_DELETE;
536 tblk->u.ip = ip;
537 }
538
539 /*
540 * Incomplete truncate of file data can
541 * result in timing problems unless we synchronously commit the
542 * transaction.
543 */
544 if (new_size)
545 commit_flag = COMMIT_SYNC;
546 else
547 commit_flag = 0;
548
549 /*
550 * If xtTruncate was incomplete, commit synchronously to avoid
551 * timing complications
552 */
553 rc = txCommit(tid, 2, &iplist[0], commit_flag);
554
555 txEnd(tid);
556
Ingo Molnar1de87442006-01-24 15:22:50 -0600557 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500558 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 while (new_size && (rc == 0)) {
561 tid = txBegin(dip->i_sb, 0);
Ingo Molnar1de87442006-01-24 15:22:50 -0600562 mutex_lock(&JFS_IP(ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 new_size = xtTruncate_pmap(tid, ip, new_size);
564 if (new_size < 0) {
565 txAbort(tid, 1); /* Marks FS Dirty */
566 rc = new_size;
567 } else
568 rc = txCommit(tid, 2, &iplist[0], COMMIT_SYNC);
569 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600570 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
572
573 if (ip->i_nlink == 0)
574 set_cflag(COMMIT_Nolink, ip);
575
576 IWRITE_UNLOCK(ip);
577
578 /*
579 * Truncating the directory index table is not guaranteed. It
580 * may need to be done iteratively
581 */
582 if (test_cflag(COMMIT_Stale, dip)) {
583 if (dip->i_size > 1)
584 jfs_truncate_nolock(dip, 0);
585
586 clear_cflag(COMMIT_Stale, dip);
587 }
588
589 out1:
590 free_UCSname(&dname);
591 out:
592 jfs_info("jfs_unlink: rc:%d", rc);
593 return rc;
594}
595
596/*
597 * NAME: commitZeroLink()
598 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500599 * FUNCTION: for non-directory, called by jfs_remove(),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 * truncate a regular file, directory or symbolic
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500601 * link to zero length. return 0 if type is not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 * one of these.
603 *
604 * if the file is currently associated with a VM segment
605 * only permanent disk and inode map resources are freed,
606 * and neither the inode nor indirect blocks are modified
607 * so that the resources can be later freed in the work
608 * map by ctrunc1.
609 * if there is no VM segment on entry, the resources are
610 * freed in both work and permanent map.
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500611 * (? for temporary file - memory object is cached even
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 * after no reference:
613 * reference count > 0 - )
614 *
615 * PARAMETERS: cd - pointer to commit data structure.
616 * current inode is the one to truncate.
617 *
618 * RETURN: Errors from subroutines
619 */
620static s64 commitZeroLink(tid_t tid, struct inode *ip)
621{
622 int filetype;
623 struct tblock *tblk;
624
625 jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid, ip);
626
627 filetype = ip->i_mode & S_IFMT;
628 switch (filetype) {
629 case S_IFREG:
630 break;
631 case S_IFLNK:
632 /* fast symbolic link */
633 if (ip->i_size < IDATASIZE) {
634 ip->i_size = 0;
635 return 0;
636 }
637 break;
638 default:
639 assert(filetype != S_IFDIR);
640 return 0;
641 }
642
643 set_cflag(COMMIT_Freewmap, ip);
644
645 /* mark transaction of block map update type */
646 tblk = tid_to_tblock(tid);
647 tblk->xflag |= COMMIT_PMAP;
648
649 /*
650 * free EA
651 */
652 if (JFS_IP(ip)->ea.flag & DXD_EXTENT)
653 /* acquire maplock on EA to be freed from block map */
654 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
655
656 /*
657 * free ACL
658 */
659 if (JFS_IP(ip)->acl.flag & DXD_EXTENT)
660 /* acquire maplock on EA to be freed from block map */
661 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
662
663 /*
664 * free xtree/data (truncate to zero length):
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500665 * free xtree/data pages from cache if COMMIT_PWMAP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 * free xtree/data blocks from persistent block map, and
667 * free xtree/data blocks from working block map if COMMIT_PWMAP;
668 */
669 if (ip->i_size)
670 return xtTruncate_pmap(tid, ip, 0);
671
672 return 0;
673}
674
675
676/*
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500677 * NAME: jfs_free_zero_link()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500679 * FUNCTION: for non-directory, called by iClose(),
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500680 * free resources of a file from cache and WORKING map
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 * for a file previously committed with zero link count
682 * while associated with a pager object,
683 *
684 * PARAMETER: ip - pointer to inode of file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 */
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500686void jfs_free_zero_link(struct inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 int type;
689
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500690 jfs_info("jfs_free_zero_link: ip = 0x%p", ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 /* return if not reg or symbolic link or if size is
693 * already ok.
694 */
695 type = ip->i_mode & S_IFMT;
696
697 switch (type) {
698 case S_IFREG:
699 break;
700 case S_IFLNK:
701 /* if its contained in inode nothing to do */
702 if (ip->i_size < IDATASIZE)
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500703 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 break;
705 default:
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500706 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
708
709 /*
710 * free EA
711 */
712 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
713 s64 xaddr = addressDXD(&JFS_IP(ip)->ea);
714 int xlen = lengthDXD(&JFS_IP(ip)->ea);
715 struct maplock maplock; /* maplock for COMMIT_WMAP */
716 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
717
718 /* free EA pages from cache */
719 invalidate_dxd_metapages(ip, JFS_IP(ip)->ea);
720
721 /* free EA extent from working block map */
722 maplock.index = 1;
723 pxdlock = (struct pxd_lock *) & maplock;
724 pxdlock->flag = mlckFREEPXD;
725 PXDaddress(&pxdlock->pxd, xaddr);
726 PXDlength(&pxdlock->pxd, xlen);
727 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
728 }
729
730 /*
731 * free ACL
732 */
733 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
734 s64 xaddr = addressDXD(&JFS_IP(ip)->acl);
735 int xlen = lengthDXD(&JFS_IP(ip)->acl);
736 struct maplock maplock; /* maplock for COMMIT_WMAP */
737 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
738
739 invalidate_dxd_metapages(ip, JFS_IP(ip)->acl);
740
741 /* free ACL extent from working block map */
742 maplock.index = 1;
743 pxdlock = (struct pxd_lock *) & maplock;
744 pxdlock->flag = mlckFREEPXD;
745 PXDaddress(&pxdlock->pxd, xaddr);
746 PXDlength(&pxdlock->pxd, xlen);
747 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
748 }
749
750 /*
751 * free xtree/data (truncate to zero length):
752 * free xtree/data pages from cache, and
753 * free xtree/data blocks from working block map;
754 */
755 if (ip->i_size)
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500756 xtTruncate(0, ip, 0, COMMIT_WMAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
759/*
760 * NAME: jfs_link(vp, dvp, name, crp)
761 *
762 * FUNCTION: create a link to <vp> by the name = <name>
763 * in the parent directory <dvp>
764 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500765 * PARAMETER: vp - target object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 * dvp - parent directory of new link
767 * name - name of new link to target object
768 * crp - credential
769 *
770 * RETURN: Errors from subroutines
771 *
772 * note:
773 * JFS does NOT support link() on directories (to prevent circular
774 * path in the directory hierarchy);
775 * EPERM: the target object is a directory, and either the caller
776 * does not have appropriate privileges or the implementation prohibits
777 * using link() on directories [XPG4.2].
778 *
779 * JFS does NOT support links between file systems:
780 * EXDEV: target object and new link are on different file systems and
781 * implementation does not support links between file systems [XPG4.2].
782 */
783static int jfs_link(struct dentry *old_dentry,
784 struct inode *dir, struct dentry *dentry)
785{
786 int rc;
787 tid_t tid;
788 struct inode *ip = old_dentry->d_inode;
789 ino_t ino;
790 struct component_name dname;
791 struct btstack btstack;
792 struct inode *iplist[2];
793
794 jfs_info("jfs_link: %s %s", old_dentry->d_name.name,
795 dentry->d_name.name);
796
797 if (ip->i_nlink == JFS_LINK_MAX)
798 return -EMLINK;
799
800 if (ip->i_nlink == 0)
801 return -ENOENT;
802
803 tid = txBegin(ip->i_sb, 0);
804
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600805 mutex_lock_nested(&JFS_IP(dir)->commit_mutex, COMMIT_MUTEX_PARENT);
806 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 /*
809 * scan parent directory for entry/freespace
810 */
811 if ((rc = get_UCSname(&dname, dentry)))
812 goto out;
813
814 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
815 goto free_dname;
816
817 /*
818 * create entry for new link in parent directory
819 */
820 ino = ip->i_ino;
821 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
822 goto free_dname;
823
824 /* update object inode */
Dave Hansend8c76e62006-09-30 23:29:04 -0700825 inc_nlink(ip); /* for new link */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 ip->i_ctime = CURRENT_TIME;
Dave Kleikamp988a6492005-10-31 16:53:04 -0600827 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 mark_inode_dirty(dir);
829 atomic_inc(&ip->i_count);
830
831 iplist[0] = ip;
832 iplist[1] = dir;
833 rc = txCommit(tid, 2, &iplist[0], 0);
834
835 if (rc) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700836 ip->i_nlink--; /* never instantiated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 iput(ip);
838 } else
839 d_instantiate(dentry, ip);
840
841 free_dname:
842 free_UCSname(&dname);
843
844 out:
845 txEnd(tid);
846
Ingo Molnar1de87442006-01-24 15:22:50 -0600847 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500848 mutex_unlock(&JFS_IP(dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 jfs_info("jfs_link: rc:%d", rc);
851 return rc;
852}
853
854/*
855 * NAME: jfs_symlink(dip, dentry, name)
856 *
857 * FUNCTION: creates a symbolic link to <symlink> by name <name>
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500858 * in directory <dip>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500860 * PARAMETER: dip - parent directory vnode
861 * dentry - dentry of symbolic link
862 * name - the path name of the existing object
863 * that will be the source of the link
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 *
865 * RETURN: errors from subroutines
866 *
867 * note:
868 * ENAMETOOLONG: pathname resolution of a symbolic link produced
869 * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
870*/
871
872static int jfs_symlink(struct inode *dip, struct dentry *dentry,
873 const char *name)
874{
875 int rc;
876 tid_t tid;
877 ino_t ino = 0;
878 struct component_name dname;
879 int ssize; /* source pathname size */
880 struct btstack btstack;
881 struct inode *ip = dentry->d_inode;
882 unchar *i_fastsymlink;
883 s64 xlen = 0;
884 int bmask = 0, xsize;
885 s64 extent = 0, xaddr;
886 struct metapage *mp;
887 struct super_block *sb;
888 struct tblock *tblk;
889
890 struct inode *iplist[2];
891
892 jfs_info("jfs_symlink: dip:0x%p name:%s", dip, name);
893
894 ssize = strlen(name) + 1;
895
896 /*
897 * search parent directory for entry/freespace
898 * (dtSearch() returns parent directory page pinned)
899 */
900
901 if ((rc = get_UCSname(&dname, dentry)))
902 goto out1;
903
904 /*
905 * allocate on-disk/in-memory inode for symbolic link:
906 * (iAlloc() returns new, locked inode)
907 */
908 ip = ialloc(dip, S_IFLNK | 0777);
Akinobu Mita087387f2006-09-14 09:22:38 -0500909 if (IS_ERR(ip)) {
910 rc = PTR_ERR(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 goto out2;
912 }
913
914 tid = txBegin(dip->i_sb, 0);
915
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600916 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
917 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Dave Kleikamp1d15b10f2005-09-01 09:05:39 -0500919 rc = jfs_init_security(tid, ip, dip);
920 if (rc)
921 goto out3;
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 tblk = tid_to_tblock(tid);
924 tblk->xflag |= COMMIT_CREATE;
925 tblk->ino = ip->i_ino;
926 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
927
928 /* fix symlink access permission
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500929 * (dir_create() ANDs in the u.u_cmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 * but symlinks really need to be 777 access)
931 */
932 ip->i_mode |= 0777;
933
934 /*
935 * write symbolic link target path name
936 */
937 xtInitRoot(tid, ip);
938
939 /*
940 * write source path name inline in on-disk inode (fast symbolic link)
941 */
942
943 if (ssize <= IDATASIZE) {
944 ip->i_op = &jfs_symlink_inode_operations;
945
946 i_fastsymlink = JFS_IP(ip)->i_inline;
947 memcpy(i_fastsymlink, name, ssize);
948 ip->i_size = ssize - 1;
949
950 /*
951 * if symlink is > 128 bytes, we don't have the space to
952 * store inline extended attributes
953 */
954 if (ssize > sizeof (JFS_IP(ip)->i_inline))
955 JFS_IP(ip)->mode2 &= ~INLINEEA;
956
957 jfs_info("jfs_symlink: fast symlink added ssize:%d name:%s ",
958 ssize, name);
959 }
960 /*
961 * write source path name in a single extent
962 */
963 else {
964 jfs_info("jfs_symlink: allocate extent ip:0x%p", ip);
965
966 ip->i_op = &page_symlink_inode_operations;
967 ip->i_mapping->a_ops = &jfs_aops;
968
969 /*
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500970 * even though the data of symlink object (source
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 * path name) is treated as non-journaled user data,
972 * it is read/written thru buffer cache for performance.
973 */
974 sb = ip->i_sb;
975 bmask = JFS_SBI(sb)->bsize - 1;
976 xsize = (ssize + bmask) & ~bmask;
977 xaddr = 0;
978 xlen = xsize >> JFS_SBI(sb)->l2bsize;
979 if ((rc = xtInsert(tid, ip, 0, 0, xlen, &xaddr, 0))) {
980 txAbort(tid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 goto out3;
982 }
983 extent = xaddr;
984 ip->i_size = ssize - 1;
985 while (ssize) {
986 /* This is kind of silly since PATH_MAX == 4K */
987 int copy_size = min(ssize, PSIZE);
988
989 mp = get_metapage(ip, xaddr, PSIZE, 1);
990
991 if (mp == NULL) {
992 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
993 rc = -EIO;
994 txAbort(tid, 0);
995 goto out3;
996 }
997 memcpy(mp->data, name, copy_size);
998 flush_metapage(mp);
999 ssize -= copy_size;
1000 name += copy_size;
1001 xaddr += JFS_SBI(sb)->nbperpage;
1002 }
1003 }
1004
1005 /*
1006 * create entry for symbolic link in parent directory
1007 */
1008 rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE);
1009 if (rc == 0) {
1010 ino = ip->i_ino;
1011 rc = dtInsert(tid, dip, &dname, &ino, &btstack);
1012 }
1013 if (rc) {
1014 if (xlen)
1015 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
1016 txAbort(tid, 0);
1017 /* discard new inode */
1018 goto out3;
1019 }
1020
1021 insert_inode_hash(ip);
1022 mark_inode_dirty(ip);
1023
Dave Kleikamp988a6492005-10-31 16:53:04 -06001024 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
1025 mark_inode_dirty(dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 /*
1027 * commit update of parent directory and link object
1028 */
1029
1030 iplist[0] = dip;
1031 iplist[1] = ip;
1032 rc = txCommit(tid, 2, &iplist[0], 0);
1033
1034 out3:
1035 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -06001036 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -05001037 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (rc) {
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001039 free_ea_wmap(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 ip->i_nlink = 0;
1041 iput(ip);
1042 } else
1043 d_instantiate(dentry, ip);
1044
1045 out2:
1046 free_UCSname(&dname);
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 out1:
1049 jfs_info("jfs_symlink: rc:%d", rc);
1050 return rc;
1051}
1052
1053
1054/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001055 * NAME: jfs_rename
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001057 * FUNCTION: rename a file or directory
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 */
1059static int jfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1060 struct inode *new_dir, struct dentry *new_dentry)
1061{
1062 struct btstack btstack;
1063 ino_t ino;
1064 struct component_name new_dname;
1065 struct inode *new_ip;
1066 struct component_name old_dname;
1067 struct inode *old_ip;
1068 int rc;
1069 tid_t tid;
1070 struct tlock *tlck;
1071 struct dt_lock *dtlck;
1072 struct lv *lv;
1073 int ipcount;
1074 struct inode *iplist[4];
1075 struct tblock *tblk;
1076 s64 new_size = 0;
1077 int commit_flag;
1078
1079
1080 jfs_info("jfs_rename: %s %s", old_dentry->d_name.name,
1081 new_dentry->d_name.name);
1082
1083 old_ip = old_dentry->d_inode;
1084 new_ip = new_dentry->d_inode;
1085
1086 if ((rc = get_UCSname(&old_dname, old_dentry)))
1087 goto out1;
1088
1089 if ((rc = get_UCSname(&new_dname, new_dentry)))
1090 goto out2;
1091
1092 /*
1093 * Make sure source inode number is what we think it is
1094 */
1095 rc = dtSearch(old_dir, &old_dname, &ino, &btstack, JFS_LOOKUP);
1096 if (rc || (ino != old_ip->i_ino)) {
1097 rc = -ENOENT;
1098 goto out3;
1099 }
1100
1101 /*
1102 * Make sure dest inode number (if any) is what we think it is
1103 */
1104 rc = dtSearch(new_dir, &new_dname, &ino, &btstack, JFS_LOOKUP);
1105 if (rc == 0) {
1106 if ((new_ip == 0) || (ino != new_ip->i_ino)) {
1107 rc = -ESTALE;
1108 goto out3;
1109 }
1110 } else if (rc != -ENOENT)
1111 goto out3;
1112 else if (new_ip) {
1113 /* no entry exists, but one was expected */
1114 rc = -ESTALE;
1115 goto out3;
1116 }
1117
1118 if (S_ISDIR(old_ip->i_mode)) {
1119 if (new_ip) {
1120 if (!dtEmpty(new_ip)) {
1121 rc = -ENOTEMPTY;
1122 goto out3;
1123 }
1124 } else if ((new_dir != old_dir) &&
1125 (new_dir->i_nlink == JFS_LINK_MAX)) {
1126 rc = -EMLINK;
1127 goto out3;
1128 }
1129 } else if (new_ip) {
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001130 IWRITE_LOCK(new_ip, RDWRLOCK_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 /* Init inode for quota operations. */
1132 DQUOT_INIT(new_ip);
1133 }
1134
1135 /*
1136 * The real work starts here
1137 */
1138 tid = txBegin(new_dir->i_sb, 0);
1139
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001140 /*
1141 * How do we know the locking is safe from deadlocks?
1142 * The vfs does the hard part for us. Any time we are taking nested
1143 * commit_mutexes, the vfs already has i_mutex held on the parent.
1144 * Here, the vfs has already taken i_mutex on both old_dir and new_dir.
1145 */
1146 mutex_lock_nested(&JFS_IP(new_dir)->commit_mutex, COMMIT_MUTEX_PARENT);
1147 mutex_lock_nested(&JFS_IP(old_ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 if (old_dir != new_dir)
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001149 mutex_lock_nested(&JFS_IP(old_dir)->commit_mutex,
1150 COMMIT_MUTEX_SECOND_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 if (new_ip) {
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001153 mutex_lock_nested(&JFS_IP(new_ip)->commit_mutex,
1154 COMMIT_MUTEX_VICTIM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 /*
1156 * Change existing directory entry to new inode number
1157 */
1158 ino = new_ip->i_ino;
1159 rc = dtModify(tid, new_dir, &new_dname, &ino,
1160 old_ip->i_ino, JFS_RENAME);
1161 if (rc)
1162 goto out4;
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001163 drop_nlink(new_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (S_ISDIR(new_ip->i_mode)) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001165 drop_nlink(new_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (new_ip->i_nlink) {
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -05001167 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (old_dir != new_dir)
Ingo Molnar1de87442006-01-24 15:22:50 -06001169 mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -05001170 mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
1171 mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (!S_ISDIR(old_ip->i_mode) && new_ip)
1173 IWRITE_UNLOCK(new_ip);
1174 jfs_error(new_ip->i_sb,
1175 "jfs_rename: new_ip->i_nlink != 0");
1176 return -EIO;
1177 }
1178 tblk = tid_to_tblock(tid);
1179 tblk->xflag |= COMMIT_DELETE;
1180 tblk->u.ip = new_ip;
1181 } else if (new_ip->i_nlink == 0) {
1182 assert(!test_cflag(COMMIT_Nolink, new_ip));
1183 /* free block resources */
1184 if ((new_size = commitZeroLink(tid, new_ip)) < 0) {
1185 txAbort(tid, 1); /* Marks FS Dirty */
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001186 rc = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 goto out4;
1188 }
1189 tblk = tid_to_tblock(tid);
1190 tblk->xflag |= COMMIT_DELETE;
1191 tblk->u.ip = new_ip;
1192 } else {
1193 new_ip->i_ctime = CURRENT_TIME;
1194 mark_inode_dirty(new_ip);
1195 }
1196 } else {
1197 /*
1198 * Add new directory entry
1199 */
1200 rc = dtSearch(new_dir, &new_dname, &ino, &btstack,
1201 JFS_CREATE);
1202 if (rc) {
1203 jfs_err("jfs_rename didn't expect dtSearch to fail "
1204 "w/rc = %d", rc);
1205 goto out4;
1206 }
1207
1208 ino = old_ip->i_ino;
1209 rc = dtInsert(tid, new_dir, &new_dname, &ino, &btstack);
1210 if (rc) {
1211 if (rc == -EIO)
1212 jfs_err("jfs_rename: dtInsert returned -EIO");
1213 goto out4;
1214 }
1215 if (S_ISDIR(old_ip->i_mode))
Dave Hansend8c76e62006-09-30 23:29:04 -07001216 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 }
1218 /*
1219 * Remove old directory entry
1220 */
1221
1222 ino = old_ip->i_ino;
1223 rc = dtDelete(tid, old_dir, &old_dname, &ino, JFS_REMOVE);
1224 if (rc) {
1225 jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
1226 rc);
1227 txAbort(tid, 1); /* Marks Filesystem dirty */
1228 goto out4;
1229 }
1230 if (S_ISDIR(old_ip->i_mode)) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001231 drop_nlink(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (old_dir != new_dir) {
1233 /*
1234 * Change inode number of parent for moved directory
1235 */
1236
1237 JFS_IP(old_ip)->i_dtroot.header.idotdot =
1238 cpu_to_le32(new_dir->i_ino);
1239
1240 /* Linelock header of dtree */
1241 tlck = txLock(tid, old_ip,
1242 (struct metapage *) &JFS_IP(old_ip)->bxflag,
1243 tlckDTREE | tlckBTROOT | tlckRELINK);
1244 dtlck = (struct dt_lock *) & tlck->lock;
1245 ASSERT(dtlck->index == 0);
1246 lv = & dtlck->lv[0];
1247 lv->offset = 0;
1248 lv->length = 1;
1249 dtlck->index++;
1250 }
1251 }
1252
1253 /*
1254 * Update ctime on changed/moved inodes & mark dirty
1255 */
1256 old_ip->i_ctime = CURRENT_TIME;
1257 mark_inode_dirty(old_ip);
1258
1259 new_dir->i_ctime = new_dir->i_mtime = current_fs_time(new_dir->i_sb);
1260 mark_inode_dirty(new_dir);
1261
1262 /* Build list of inodes modified by this transaction */
1263 ipcount = 0;
1264 iplist[ipcount++] = old_ip;
1265 if (new_ip)
1266 iplist[ipcount++] = new_ip;
1267 iplist[ipcount++] = old_dir;
1268
1269 if (old_dir != new_dir) {
1270 iplist[ipcount++] = new_dir;
1271 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1272 mark_inode_dirty(old_dir);
1273 }
1274
1275 /*
1276 * Incomplete truncate of file data can
1277 * result in timing problems unless we synchronously commit the
1278 * transaction.
1279 */
1280 if (new_size)
1281 commit_flag = COMMIT_SYNC;
1282 else
1283 commit_flag = 0;
1284
1285 rc = txCommit(tid, ipcount, iplist, commit_flag);
1286
1287 out4:
1288 txEnd(tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (new_ip)
Ingo Molnar1de87442006-01-24 15:22:50 -06001290 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -05001291 if (old_dir != new_dir)
1292 mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
1293 mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
1294 mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 while (new_size && (rc == 0)) {
1297 tid = txBegin(new_ip->i_sb, 0);
Ingo Molnar1de87442006-01-24 15:22:50 -06001298 mutex_lock(&JFS_IP(new_ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 new_size = xtTruncate_pmap(tid, new_ip, new_size);
1300 if (new_size < 0) {
1301 txAbort(tid, 1);
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001302 rc = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 } else
1304 rc = txCommit(tid, 1, &new_ip, COMMIT_SYNC);
1305 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -06001306 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 }
1308 if (new_ip && (new_ip->i_nlink == 0))
1309 set_cflag(COMMIT_Nolink, new_ip);
1310 out3:
1311 free_UCSname(&new_dname);
1312 out2:
1313 free_UCSname(&old_dname);
1314 out1:
1315 if (new_ip && !S_ISDIR(new_ip->i_mode))
1316 IWRITE_UNLOCK(new_ip);
1317 /*
1318 * Truncating the directory index table is not guaranteed. It
1319 * may need to be done iteratively
1320 */
1321 if (test_cflag(COMMIT_Stale, old_dir)) {
1322 if (old_dir->i_size > 1)
1323 jfs_truncate_nolock(old_dir, 0);
1324
1325 clear_cflag(COMMIT_Stale, old_dir);
1326 }
1327
1328 jfs_info("jfs_rename: returning %d", rc);
1329 return rc;
1330}
1331
1332
1333/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001334 * NAME: jfs_mknod
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001336 * FUNCTION: Create a special file (device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 */
1338static int jfs_mknod(struct inode *dir, struct dentry *dentry,
1339 int mode, dev_t rdev)
1340{
1341 struct jfs_inode_info *jfs_ip;
1342 struct btstack btstack;
1343 struct component_name dname;
1344 ino_t ino;
1345 struct inode *ip;
1346 struct inode *iplist[2];
1347 int rc;
1348 tid_t tid;
1349 struct tblock *tblk;
1350
1351 if (!new_valid_dev(rdev))
1352 return -EINVAL;
1353
1354 jfs_info("jfs_mknod: %s", dentry->d_name.name);
1355
1356 if ((rc = get_UCSname(&dname, dentry)))
1357 goto out;
1358
1359 ip = ialloc(dir, mode);
Akinobu Mita087387f2006-09-14 09:22:38 -05001360 if (IS_ERR(ip)) {
1361 rc = PTR_ERR(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 goto out1;
1363 }
1364 jfs_ip = JFS_IP(ip);
1365
1366 tid = txBegin(dir->i_sb, 0);
1367
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001368 mutex_lock_nested(&JFS_IP(dir)->commit_mutex, COMMIT_MUTEX_PARENT);
1369 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001371 rc = jfs_init_acl(tid, ip, dir);
1372 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 goto out3;
1374
Dave Kleikamp1d15b10f2005-09-01 09:05:39 -05001375 rc = jfs_init_security(tid, ip, dir);
1376 if (rc) {
1377 txAbort(tid, 0);
1378 goto out3;
1379 }
1380
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001381 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE))) {
1382 txAbort(tid, 0);
1383 goto out3;
1384 }
1385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 tblk = tid_to_tblock(tid);
1387 tblk->xflag |= COMMIT_CREATE;
1388 tblk->ino = ip->i_ino;
1389 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
1390
1391 ino = ip->i_ino;
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001392 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack))) {
1393 txAbort(tid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 goto out3;
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 ip->i_op = &jfs_file_inode_operations;
1398 jfs_ip->dev = new_encode_dev(rdev);
1399 init_special_inode(ip, ip->i_mode, rdev);
1400
1401 insert_inode_hash(ip);
1402 mark_inode_dirty(ip);
1403
1404 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1405
1406 mark_inode_dirty(dir);
1407
1408 iplist[0] = dir;
1409 iplist[1] = ip;
1410 rc = txCommit(tid, 2, iplist, 0);
1411
1412 out3:
1413 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -06001414 mutex_unlock(&JFS_IP(ip)->commit_mutex);
1415 mutex_unlock(&JFS_IP(dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if (rc) {
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001417 free_ea_wmap(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 ip->i_nlink = 0;
1419 iput(ip);
1420 } else
1421 d_instantiate(dentry, ip);
1422
1423 out1:
1424 free_UCSname(&dname);
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 out:
1427 jfs_info("jfs_mknod: returning %d", rc);
1428 return rc;
1429}
1430
1431static struct dentry *jfs_lookup(struct inode *dip, struct dentry *dentry, struct nameidata *nd)
1432{
1433 struct btstack btstack;
1434 ino_t inum;
1435 struct inode *ip;
1436 struct component_name key;
1437 const char *name = dentry->d_name.name;
1438 int len = dentry->d_name.len;
1439 int rc;
1440
1441 jfs_info("jfs_lookup: name = %s", name);
1442
Dave Kleikamp686762c2005-08-17 13:53:13 -05001443 if (JFS_SBI(dip->i_sb)->mntflag & JFS_OS2)
1444 dentry->d_op = &jfs_ci_dentry_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 if ((name[0] == '.') && (len == 1))
1447 inum = dip->i_ino;
1448 else if (strcmp(name, "..") == 0)
1449 inum = PARENT(dip);
1450 else {
1451 if ((rc = get_UCSname(&key, dentry)))
1452 return ERR_PTR(rc);
1453 rc = dtSearch(dip, &key, &inum, &btstack, JFS_LOOKUP);
1454 free_UCSname(&key);
1455 if (rc == -ENOENT) {
1456 d_add(dentry, NULL);
1457 return ERR_PTR(0);
1458 } else if (rc) {
1459 jfs_err("jfs_lookup: dtSearch returned %d", rc);
1460 return ERR_PTR(rc);
1461 }
1462 }
1463
1464 ip = iget(dip->i_sb, inum);
1465 if (ip == NULL || is_bad_inode(ip)) {
1466 jfs_err("jfs_lookup: iget failed on inum %d", (uint) inum);
1467 if (ip)
1468 iput(ip);
1469 return ERR_PTR(-EACCES);
1470 }
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 dentry = d_splice_alias(ip, dentry);
1473
1474 if (dentry && (JFS_SBI(dip->i_sb)->mntflag & JFS_OS2))
1475 dentry->d_op = &jfs_ci_dentry_operations;
1476
1477 return dentry;
1478}
1479
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001480struct dentry *jfs_get_dentry(struct super_block *sb, void *vobjp)
1481{
1482 __u32 *objp = vobjp;
1483 unsigned long ino = objp[0];
1484 __u32 generation = objp[1];
1485 struct inode *inode;
1486 struct dentry *result;
1487
1488 if (ino == 0)
1489 return ERR_PTR(-ESTALE);
1490 inode = iget(sb, ino);
1491 if (inode == NULL)
1492 return ERR_PTR(-ENOMEM);
1493
1494 if (is_bad_inode(inode) ||
1495 (generation && inode->i_generation != generation)) {
1496 result = ERR_PTR(-ESTALE);
1497 goto out_iput;
1498 }
1499
1500 result = d_alloc_anon(inode);
1501 if (!result) {
1502 result = ERR_PTR(-ENOMEM);
1503 goto out_iput;
1504 }
1505 return result;
1506
1507 out_iput:
1508 iput(inode);
1509 return result;
1510}
1511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512struct dentry *jfs_get_parent(struct dentry *dentry)
1513{
1514 struct super_block *sb = dentry->d_inode->i_sb;
1515 struct dentry *parent = ERR_PTR(-ENOENT);
1516 struct inode *inode;
1517 unsigned long parent_ino;
1518
1519 parent_ino =
1520 le32_to_cpu(JFS_IP(dentry->d_inode)->i_dtroot.header.idotdot);
1521 inode = iget(sb, parent_ino);
1522 if (inode) {
1523 if (is_bad_inode(inode)) {
1524 iput(inode);
1525 parent = ERR_PTR(-EACCES);
1526 } else {
1527 parent = d_alloc_anon(inode);
1528 if (!parent) {
1529 parent = ERR_PTR(-ENOMEM);
1530 iput(inode);
1531 }
1532 }
1533 }
1534
1535 return parent;
1536}
1537
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001538const struct inode_operations jfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 .create = jfs_create,
1540 .lookup = jfs_lookup,
1541 .link = jfs_link,
1542 .unlink = jfs_unlink,
1543 .symlink = jfs_symlink,
1544 .mkdir = jfs_mkdir,
1545 .rmdir = jfs_rmdir,
1546 .mknod = jfs_mknod,
1547 .rename = jfs_rename,
1548 .setxattr = jfs_setxattr,
1549 .getxattr = jfs_getxattr,
1550 .listxattr = jfs_listxattr,
1551 .removexattr = jfs_removexattr,
1552#ifdef CONFIG_JFS_POSIX_ACL
1553 .setattr = jfs_setattr,
1554 .permission = jfs_permission,
1555#endif
1556};
1557
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001558const struct file_operations jfs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 .read = generic_read_dir,
1560 .readdir = jfs_readdir,
1561 .fsync = jfs_fsync,
Herbert Poetzlfa3241d2006-02-09 09:09:16 -06001562 .ioctl = jfs_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563};
1564
1565static int jfs_ci_hash(struct dentry *dir, struct qstr *this)
1566{
1567 unsigned long hash;
1568 int i;
1569
1570 hash = init_name_hash();
1571 for (i=0; i < this->len; i++)
1572 hash = partial_name_hash(tolower(this->name[i]), hash);
1573 this->hash = end_name_hash(hash);
1574
1575 return 0;
1576}
1577
1578static int jfs_ci_compare(struct dentry *dir, struct qstr *a, struct qstr *b)
1579{
1580 int i, result = 1;
1581
1582 if (a->len != b->len)
1583 goto out;
1584 for (i=0; i < a->len; i++) {
1585 if (tolower(a->name[i]) != tolower(b->name[i]))
1586 goto out;
1587 }
1588 result = 0;
1589
1590 /*
1591 * We want creates to preserve case. A negative dentry, a, that
1592 * has a different case than b may cause a new entry to be created
1593 * with the wrong case. Since we can't tell if a comes from a negative
1594 * dentry, we blindly replace it with b. This should be harmless if
1595 * a is not a negative dentry.
1596 */
1597 memcpy((unsigned char *)a->name, b->name, a->len);
1598out:
1599 return result;
1600}
1601
1602struct dentry_operations jfs_ci_dentry_operations =
1603{
1604 .d_hash = jfs_ci_hash,
1605 .d_compare = jfs_ci_compare,
1606};