blob: 295268ad231b5ec1f26c78cb43489d434122c907 [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
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * 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
16 * along with this program; if not, write to the Free Software
17 * 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 *
44 * FUNCTION: free uncommitted extended attributes from working map
45 *
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 *
65 * PARAMETER: dip - parent directory vnode
66 * 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);
100 if (ip == NULL) {
101 rc = -ENOSPC;
102 goto out2;
103 }
104
105 tid = txBegin(dip->i_sb, 0);
106
Ingo Molnar1de87442006-01-24 15:22:50 -0600107 mutex_lock(&JFS_IP(dip)->commit_mutex);
108 mutex_lock(&JFS_IP(ip)->commit_mutex);
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 *
193 * PARAMETER: dip - parent directory vnode
194 * 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);
234 if (ip == NULL) {
235 rc = -ENOSPC;
236 goto out2;
237 }
238
239 tid = txBegin(dip->i_sb, 0);
240
Ingo Molnar1de87442006-01-24 15:22:50 -0600241 mutex_lock(&JFS_IP(dip)->commit_mutex);
242 mutex_lock(&JFS_IP(ip)->commit_mutex);
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 */
295 dip->i_nlink++; /* for '..' from child directory */
296 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 *
327 * PARAMETER: dip - parent inode
328 * dentry - child directory dentry
329 *
330 * RETURN: -EINVAL - if name is . or ..
331 * -EINVAL - if . or .. exist but are invalid.
332 * errors from subroutines
333 *
334 * note:
335 * 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
339 * 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
Ingo Molnar1de87442006-01-24 15:22:50 -0600368 mutex_lock(&JFS_IP(dip)->commit_mutex);
369 mutex_lock(&JFS_IP(ip)->commit_mutex);
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 */
396 dip->i_nlink--;
397 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
398 mark_inode_dirty(dip);
399
400 /*
401 * OS/2 could have created EA and/or ACL
402 */
403 /* free EA from both persistent and working map */
404 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
405 /* free EA pages */
406 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
407 }
408 JFS_IP(ip)->ea.flag = 0;
409
410 /* free ACL from both persistent and working map */
411 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
412 /* free ACL pages */
413 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
414 }
415 JFS_IP(ip)->acl.flag = 0;
416
417 /* mark the target directory as deleted */
418 ip->i_nlink = 0;
419 mark_inode_dirty(ip);
420
421 rc = txCommit(tid, 2, &iplist[0], 0);
422
423 txEnd(tid);
424
Ingo Molnar1de87442006-01-24 15:22:50 -0600425 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500426 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 /*
429 * Truncating the directory index table is not guaranteed. It
430 * may need to be done iteratively
431 */
432 if (test_cflag(COMMIT_Stale, dip)) {
433 if (dip->i_size > 1)
434 jfs_truncate_nolock(dip, 0);
435
436 clear_cflag(COMMIT_Stale, dip);
437 }
438
439 out2:
440 free_UCSname(&dname);
441
442 out:
443 jfs_info("jfs_rmdir: rc:%d", rc);
444 return rc;
445}
446
447/*
448 * NAME: jfs_unlink(dip, dentry)
449 *
450 * FUNCTION: remove a link to object <vp> named by <name>
451 * from parent directory <dvp>
452 *
453 * PARAMETER: dip - inode of parent directory
454 * dentry - dentry of object to be removed
455 *
456 * RETURN: errors from subroutines
457 *
458 * note:
459 * temporary file: if one or more processes have the file open
460 * when the last link is removed, the link will be removed before
461 * unlink() returns, but the removal of the file contents will be
462 * postponed until all references to the files are closed.
463 *
464 * JFS does NOT support unlink() on directories.
465 *
466 */
467static int jfs_unlink(struct inode *dip, struct dentry *dentry)
468{
469 int rc;
470 tid_t tid; /* transaction id */
471 struct inode *ip = dentry->d_inode;
472 ino_t ino;
473 struct component_name dname; /* object name */
474 struct inode *iplist[2];
475 struct tblock *tblk;
476 s64 new_size = 0;
477 int commit_flag;
478
479 jfs_info("jfs_unlink: dip:0x%p name:%s", dip, dentry->d_name.name);
480
481 /* Init inode for quota operations. */
482 DQUOT_INIT(ip);
483
484 if ((rc = get_UCSname(&dname, dentry)))
485 goto out;
486
487 IWRITE_LOCK(ip);
488
489 tid = txBegin(dip->i_sb, 0);
490
Ingo Molnar1de87442006-01-24 15:22:50 -0600491 mutex_lock(&JFS_IP(dip)->commit_mutex);
492 mutex_lock(&JFS_IP(ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 iplist[0] = dip;
495 iplist[1] = ip;
496
497 /*
498 * delete the entry of target file from parent directory
499 */
500 ino = ip->i_ino;
501 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
502 jfs_err("jfs_unlink: dtDelete returned %d", rc);
503 if (rc == -EIO)
504 txAbort(tid, 1); /* Marks FS Dirty */
505 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600506 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500507 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 IWRITE_UNLOCK(ip);
509 goto out1;
510 }
511
512 ASSERT(ip->i_nlink);
513
514 ip->i_ctime = dip->i_ctime = dip->i_mtime = CURRENT_TIME;
515 mark_inode_dirty(dip);
516
517 /* update target's inode */
518 ip->i_nlink--;
519 mark_inode_dirty(ip);
520
521 /*
522 * commit zero link count object
523 */
524 if (ip->i_nlink == 0) {
525 assert(!test_cflag(COMMIT_Nolink, ip));
526 /* free block resources */
527 if ((new_size = commitZeroLink(tid, ip)) < 0) {
528 txAbort(tid, 1); /* Marks FS Dirty */
529 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600530 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500531 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 IWRITE_UNLOCK(ip);
533 rc = new_size;
534 goto out1;
535 }
536 tblk = tid_to_tblock(tid);
537 tblk->xflag |= COMMIT_DELETE;
538 tblk->u.ip = ip;
539 }
540
541 /*
542 * Incomplete truncate of file data can
543 * result in timing problems unless we synchronously commit the
544 * transaction.
545 */
546 if (new_size)
547 commit_flag = COMMIT_SYNC;
548 else
549 commit_flag = 0;
550
551 /*
552 * If xtTruncate was incomplete, commit synchronously to avoid
553 * timing complications
554 */
555 rc = txCommit(tid, 2, &iplist[0], commit_flag);
556
557 txEnd(tid);
558
Ingo Molnar1de87442006-01-24 15:22:50 -0600559 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500560 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 while (new_size && (rc == 0)) {
563 tid = txBegin(dip->i_sb, 0);
Ingo Molnar1de87442006-01-24 15:22:50 -0600564 mutex_lock(&JFS_IP(ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 new_size = xtTruncate_pmap(tid, ip, new_size);
566 if (new_size < 0) {
567 txAbort(tid, 1); /* Marks FS Dirty */
568 rc = new_size;
569 } else
570 rc = txCommit(tid, 2, &iplist[0], COMMIT_SYNC);
571 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600572 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
574
575 if (ip->i_nlink == 0)
576 set_cflag(COMMIT_Nolink, ip);
577
578 IWRITE_UNLOCK(ip);
579
580 /*
581 * Truncating the directory index table is not guaranteed. It
582 * may need to be done iteratively
583 */
584 if (test_cflag(COMMIT_Stale, dip)) {
585 if (dip->i_size > 1)
586 jfs_truncate_nolock(dip, 0);
587
588 clear_cflag(COMMIT_Stale, dip);
589 }
590
591 out1:
592 free_UCSname(&dname);
593 out:
594 jfs_info("jfs_unlink: rc:%d", rc);
595 return rc;
596}
597
598/*
599 * NAME: commitZeroLink()
600 *
601 * FUNCTION: for non-directory, called by jfs_remove(),
602 * truncate a regular file, directory or symbolic
603 * link to zero length. return 0 if type is not
604 * one of these.
605 *
606 * if the file is currently associated with a VM segment
607 * only permanent disk and inode map resources are freed,
608 * and neither the inode nor indirect blocks are modified
609 * so that the resources can be later freed in the work
610 * map by ctrunc1.
611 * if there is no VM segment on entry, the resources are
612 * freed in both work and permanent map.
613 * (? for temporary file - memory object is cached even
614 * after no reference:
615 * reference count > 0 - )
616 *
617 * PARAMETERS: cd - pointer to commit data structure.
618 * current inode is the one to truncate.
619 *
620 * RETURN: Errors from subroutines
621 */
622static s64 commitZeroLink(tid_t tid, struct inode *ip)
623{
624 int filetype;
625 struct tblock *tblk;
626
627 jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid, ip);
628
629 filetype = ip->i_mode & S_IFMT;
630 switch (filetype) {
631 case S_IFREG:
632 break;
633 case S_IFLNK:
634 /* fast symbolic link */
635 if (ip->i_size < IDATASIZE) {
636 ip->i_size = 0;
637 return 0;
638 }
639 break;
640 default:
641 assert(filetype != S_IFDIR);
642 return 0;
643 }
644
645 set_cflag(COMMIT_Freewmap, ip);
646
647 /* mark transaction of block map update type */
648 tblk = tid_to_tblock(tid);
649 tblk->xflag |= COMMIT_PMAP;
650
651 /*
652 * free EA
653 */
654 if (JFS_IP(ip)->ea.flag & DXD_EXTENT)
655 /* acquire maplock on EA to be freed from block map */
656 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
657
658 /*
659 * free ACL
660 */
661 if (JFS_IP(ip)->acl.flag & DXD_EXTENT)
662 /* acquire maplock on EA to be freed from block map */
663 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
664
665 /*
666 * free xtree/data (truncate to zero length):
667 * free xtree/data pages from cache if COMMIT_PWMAP,
668 * free xtree/data blocks from persistent block map, and
669 * free xtree/data blocks from working block map if COMMIT_PWMAP;
670 */
671 if (ip->i_size)
672 return xtTruncate_pmap(tid, ip, 0);
673
674 return 0;
675}
676
677
678/*
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500679 * NAME: jfs_free_zero_link()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 *
681 * FUNCTION: for non-directory, called by iClose(),
682 * free resources of a file from cache and WORKING map
683 * for a file previously committed with zero link count
684 * while associated with a pager object,
685 *
686 * PARAMETER: ip - pointer to inode of file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 */
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500688void jfs_free_zero_link(struct inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 int type;
691
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500692 jfs_info("jfs_free_zero_link: ip = 0x%p", ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 /* return if not reg or symbolic link or if size is
695 * already ok.
696 */
697 type = ip->i_mode & S_IFMT;
698
699 switch (type) {
700 case S_IFREG:
701 break;
702 case S_IFLNK:
703 /* if its contained in inode nothing to do */
704 if (ip->i_size < IDATASIZE)
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500705 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 break;
707 default:
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500708 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
710
711 /*
712 * free EA
713 */
714 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
715 s64 xaddr = addressDXD(&JFS_IP(ip)->ea);
716 int xlen = lengthDXD(&JFS_IP(ip)->ea);
717 struct maplock maplock; /* maplock for COMMIT_WMAP */
718 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
719
720 /* free EA pages from cache */
721 invalidate_dxd_metapages(ip, JFS_IP(ip)->ea);
722
723 /* free EA extent from working block map */
724 maplock.index = 1;
725 pxdlock = (struct pxd_lock *) & maplock;
726 pxdlock->flag = mlckFREEPXD;
727 PXDaddress(&pxdlock->pxd, xaddr);
728 PXDlength(&pxdlock->pxd, xlen);
729 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
730 }
731
732 /*
733 * free ACL
734 */
735 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
736 s64 xaddr = addressDXD(&JFS_IP(ip)->acl);
737 int xlen = lengthDXD(&JFS_IP(ip)->acl);
738 struct maplock maplock; /* maplock for COMMIT_WMAP */
739 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
740
741 invalidate_dxd_metapages(ip, JFS_IP(ip)->acl);
742
743 /* free ACL extent from working block map */
744 maplock.index = 1;
745 pxdlock = (struct pxd_lock *) & maplock;
746 pxdlock->flag = mlckFREEPXD;
747 PXDaddress(&pxdlock->pxd, xaddr);
748 PXDlength(&pxdlock->pxd, xlen);
749 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
750 }
751
752 /*
753 * free xtree/data (truncate to zero length):
754 * free xtree/data pages from cache, and
755 * free xtree/data blocks from working block map;
756 */
757 if (ip->i_size)
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500758 xtTruncate(0, ip, 0, COMMIT_WMAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
761/*
762 * NAME: jfs_link(vp, dvp, name, crp)
763 *
764 * FUNCTION: create a link to <vp> by the name = <name>
765 * in the parent directory <dvp>
766 *
767 * PARAMETER: vp - target object
768 * dvp - parent directory of new link
769 * name - name of new link to target object
770 * crp - credential
771 *
772 * RETURN: Errors from subroutines
773 *
774 * note:
775 * JFS does NOT support link() on directories (to prevent circular
776 * path in the directory hierarchy);
777 * EPERM: the target object is a directory, and either the caller
778 * does not have appropriate privileges or the implementation prohibits
779 * using link() on directories [XPG4.2].
780 *
781 * JFS does NOT support links between file systems:
782 * EXDEV: target object and new link are on different file systems and
783 * implementation does not support links between file systems [XPG4.2].
784 */
785static int jfs_link(struct dentry *old_dentry,
786 struct inode *dir, struct dentry *dentry)
787{
788 int rc;
789 tid_t tid;
790 struct inode *ip = old_dentry->d_inode;
791 ino_t ino;
792 struct component_name dname;
793 struct btstack btstack;
794 struct inode *iplist[2];
795
796 jfs_info("jfs_link: %s %s", old_dentry->d_name.name,
797 dentry->d_name.name);
798
799 if (ip->i_nlink == JFS_LINK_MAX)
800 return -EMLINK;
801
802 if (ip->i_nlink == 0)
803 return -ENOENT;
804
805 tid = txBegin(ip->i_sb, 0);
806
Ingo Molnar1de87442006-01-24 15:22:50 -0600807 mutex_lock(&JFS_IP(dir)->commit_mutex);
808 mutex_lock(&JFS_IP(ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 /*
811 * scan parent directory for entry/freespace
812 */
813 if ((rc = get_UCSname(&dname, dentry)))
814 goto out;
815
816 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
817 goto free_dname;
818
819 /*
820 * create entry for new link in parent directory
821 */
822 ino = ip->i_ino;
823 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
824 goto free_dname;
825
826 /* update object inode */
827 ip->i_nlink++; /* for new link */
828 ip->i_ctime = CURRENT_TIME;
Dave Kleikamp988a6492005-10-31 16:53:04 -0600829 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 mark_inode_dirty(dir);
831 atomic_inc(&ip->i_count);
832
833 iplist[0] = ip;
834 iplist[1] = dir;
835 rc = txCommit(tid, 2, &iplist[0], 0);
836
837 if (rc) {
838 ip->i_nlink--;
839 iput(ip);
840 } else
841 d_instantiate(dentry, ip);
842
843 free_dname:
844 free_UCSname(&dname);
845
846 out:
847 txEnd(tid);
848
Ingo Molnar1de87442006-01-24 15:22:50 -0600849 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500850 mutex_unlock(&JFS_IP(dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 jfs_info("jfs_link: rc:%d", rc);
853 return rc;
854}
855
856/*
857 * NAME: jfs_symlink(dip, dentry, name)
858 *
859 * FUNCTION: creates a symbolic link to <symlink> by name <name>
860 * in directory <dip>
861 *
862 * PARAMETER: dip - parent directory vnode
863 * dentry - dentry of symbolic link
864 * name - the path name of the existing object
865 * that will be the source of the link
866 *
867 * RETURN: errors from subroutines
868 *
869 * note:
870 * ENAMETOOLONG: pathname resolution of a symbolic link produced
871 * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
872*/
873
874static int jfs_symlink(struct inode *dip, struct dentry *dentry,
875 const char *name)
876{
877 int rc;
878 tid_t tid;
879 ino_t ino = 0;
880 struct component_name dname;
881 int ssize; /* source pathname size */
882 struct btstack btstack;
883 struct inode *ip = dentry->d_inode;
884 unchar *i_fastsymlink;
885 s64 xlen = 0;
886 int bmask = 0, xsize;
887 s64 extent = 0, xaddr;
888 struct metapage *mp;
889 struct super_block *sb;
890 struct tblock *tblk;
891
892 struct inode *iplist[2];
893
894 jfs_info("jfs_symlink: dip:0x%p name:%s", dip, name);
895
896 ssize = strlen(name) + 1;
897
898 /*
899 * search parent directory for entry/freespace
900 * (dtSearch() returns parent directory page pinned)
901 */
902
903 if ((rc = get_UCSname(&dname, dentry)))
904 goto out1;
905
906 /*
907 * allocate on-disk/in-memory inode for symbolic link:
908 * (iAlloc() returns new, locked inode)
909 */
910 ip = ialloc(dip, S_IFLNK | 0777);
911 if (ip == NULL) {
912 rc = -ENOSPC;
913 goto out2;
914 }
915
916 tid = txBegin(dip->i_sb, 0);
917
Ingo Molnar1de87442006-01-24 15:22:50 -0600918 mutex_lock(&JFS_IP(dip)->commit_mutex);
919 mutex_lock(&JFS_IP(ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Dave Kleikamp1d15b10f2005-09-01 09:05:39 -0500921 rc = jfs_init_security(tid, ip, dip);
922 if (rc)
923 goto out3;
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 tblk = tid_to_tblock(tid);
926 tblk->xflag |= COMMIT_CREATE;
927 tblk->ino = ip->i_ino;
928 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
929
930 /* fix symlink access permission
931 * (dir_create() ANDs in the u.u_cmask,
932 * but symlinks really need to be 777 access)
933 */
934 ip->i_mode |= 0777;
935
936 /*
937 * write symbolic link target path name
938 */
939 xtInitRoot(tid, ip);
940
941 /*
942 * write source path name inline in on-disk inode (fast symbolic link)
943 */
944
945 if (ssize <= IDATASIZE) {
946 ip->i_op = &jfs_symlink_inode_operations;
947
948 i_fastsymlink = JFS_IP(ip)->i_inline;
949 memcpy(i_fastsymlink, name, ssize);
950 ip->i_size = ssize - 1;
951
952 /*
953 * if symlink is > 128 bytes, we don't have the space to
954 * store inline extended attributes
955 */
956 if (ssize > sizeof (JFS_IP(ip)->i_inline))
957 JFS_IP(ip)->mode2 &= ~INLINEEA;
958
959 jfs_info("jfs_symlink: fast symlink added ssize:%d name:%s ",
960 ssize, name);
961 }
962 /*
963 * write source path name in a single extent
964 */
965 else {
966 jfs_info("jfs_symlink: allocate extent ip:0x%p", ip);
967
968 ip->i_op = &page_symlink_inode_operations;
969 ip->i_mapping->a_ops = &jfs_aops;
970
971 /*
972 * even though the data of symlink object (source
973 * path name) is treated as non-journaled user data,
974 * it is read/written thru buffer cache for performance.
975 */
976 sb = ip->i_sb;
977 bmask = JFS_SBI(sb)->bsize - 1;
978 xsize = (ssize + bmask) & ~bmask;
979 xaddr = 0;
980 xlen = xsize >> JFS_SBI(sb)->l2bsize;
981 if ((rc = xtInsert(tid, ip, 0, 0, xlen, &xaddr, 0))) {
982 txAbort(tid, 0);
983 rc = -ENOSPC;
984 goto out3;
985 }
986 extent = xaddr;
987 ip->i_size = ssize - 1;
988 while (ssize) {
989 /* This is kind of silly since PATH_MAX == 4K */
990 int copy_size = min(ssize, PSIZE);
991
992 mp = get_metapage(ip, xaddr, PSIZE, 1);
993
994 if (mp == NULL) {
995 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
996 rc = -EIO;
997 txAbort(tid, 0);
998 goto out3;
999 }
1000 memcpy(mp->data, name, copy_size);
1001 flush_metapage(mp);
1002 ssize -= copy_size;
1003 name += copy_size;
1004 xaddr += JFS_SBI(sb)->nbperpage;
1005 }
1006 }
1007
1008 /*
1009 * create entry for symbolic link in parent directory
1010 */
1011 rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE);
1012 if (rc == 0) {
1013 ino = ip->i_ino;
1014 rc = dtInsert(tid, dip, &dname, &ino, &btstack);
1015 }
1016 if (rc) {
1017 if (xlen)
1018 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
1019 txAbort(tid, 0);
1020 /* discard new inode */
1021 goto out3;
1022 }
1023
1024 insert_inode_hash(ip);
1025 mark_inode_dirty(ip);
1026
Dave Kleikamp988a6492005-10-31 16:53:04 -06001027 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
1028 mark_inode_dirty(dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 /*
1030 * commit update of parent directory and link object
1031 */
1032
1033 iplist[0] = dip;
1034 iplist[1] = ip;
1035 rc = txCommit(tid, 2, &iplist[0], 0);
1036
1037 out3:
1038 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -06001039 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -05001040 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 if (rc) {
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001042 free_ea_wmap(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 ip->i_nlink = 0;
1044 iput(ip);
1045 } else
1046 d_instantiate(dentry, ip);
1047
1048 out2:
1049 free_UCSname(&dname);
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 out1:
1052 jfs_info("jfs_symlink: rc:%d", rc);
1053 return rc;
1054}
1055
1056
1057/*
1058 * NAME: jfs_rename
1059 *
1060 * FUNCTION: rename a file or directory
1061 */
1062static int jfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1063 struct inode *new_dir, struct dentry *new_dentry)
1064{
1065 struct btstack btstack;
1066 ino_t ino;
1067 struct component_name new_dname;
1068 struct inode *new_ip;
1069 struct component_name old_dname;
1070 struct inode *old_ip;
1071 int rc;
1072 tid_t tid;
1073 struct tlock *tlck;
1074 struct dt_lock *dtlck;
1075 struct lv *lv;
1076 int ipcount;
1077 struct inode *iplist[4];
1078 struct tblock *tblk;
1079 s64 new_size = 0;
1080 int commit_flag;
1081
1082
1083 jfs_info("jfs_rename: %s %s", old_dentry->d_name.name,
1084 new_dentry->d_name.name);
1085
1086 old_ip = old_dentry->d_inode;
1087 new_ip = new_dentry->d_inode;
1088
1089 if ((rc = get_UCSname(&old_dname, old_dentry)))
1090 goto out1;
1091
1092 if ((rc = get_UCSname(&new_dname, new_dentry)))
1093 goto out2;
1094
1095 /*
1096 * Make sure source inode number is what we think it is
1097 */
1098 rc = dtSearch(old_dir, &old_dname, &ino, &btstack, JFS_LOOKUP);
1099 if (rc || (ino != old_ip->i_ino)) {
1100 rc = -ENOENT;
1101 goto out3;
1102 }
1103
1104 /*
1105 * Make sure dest inode number (if any) is what we think it is
1106 */
1107 rc = dtSearch(new_dir, &new_dname, &ino, &btstack, JFS_LOOKUP);
1108 if (rc == 0) {
1109 if ((new_ip == 0) || (ino != new_ip->i_ino)) {
1110 rc = -ESTALE;
1111 goto out3;
1112 }
1113 } else if (rc != -ENOENT)
1114 goto out3;
1115 else if (new_ip) {
1116 /* no entry exists, but one was expected */
1117 rc = -ESTALE;
1118 goto out3;
1119 }
1120
1121 if (S_ISDIR(old_ip->i_mode)) {
1122 if (new_ip) {
1123 if (!dtEmpty(new_ip)) {
1124 rc = -ENOTEMPTY;
1125 goto out3;
1126 }
1127 } else if ((new_dir != old_dir) &&
1128 (new_dir->i_nlink == JFS_LINK_MAX)) {
1129 rc = -EMLINK;
1130 goto out3;
1131 }
1132 } else if (new_ip) {
1133 IWRITE_LOCK(new_ip);
1134 /* Init inode for quota operations. */
1135 DQUOT_INIT(new_ip);
1136 }
1137
1138 /*
1139 * The real work starts here
1140 */
1141 tid = txBegin(new_dir->i_sb, 0);
1142
Ingo Molnar1de87442006-01-24 15:22:50 -06001143 mutex_lock(&JFS_IP(new_dir)->commit_mutex);
1144 mutex_lock(&JFS_IP(old_ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (old_dir != new_dir)
Ingo Molnar1de87442006-01-24 15:22:50 -06001146 mutex_lock(&JFS_IP(old_dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 if (new_ip) {
Ingo Molnar1de87442006-01-24 15:22:50 -06001149 mutex_lock(&JFS_IP(new_ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 /*
1151 * Change existing directory entry to new inode number
1152 */
1153 ino = new_ip->i_ino;
1154 rc = dtModify(tid, new_dir, &new_dname, &ino,
1155 old_ip->i_ino, JFS_RENAME);
1156 if (rc)
1157 goto out4;
1158 new_ip->i_nlink--;
1159 if (S_ISDIR(new_ip->i_mode)) {
1160 new_ip->i_nlink--;
1161 if (new_ip->i_nlink) {
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -05001162 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 if (old_dir != new_dir)
Ingo Molnar1de87442006-01-24 15:22:50 -06001164 mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -05001165 mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
1166 mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (!S_ISDIR(old_ip->i_mode) && new_ip)
1168 IWRITE_UNLOCK(new_ip);
1169 jfs_error(new_ip->i_sb,
1170 "jfs_rename: new_ip->i_nlink != 0");
1171 return -EIO;
1172 }
1173 tblk = tid_to_tblock(tid);
1174 tblk->xflag |= COMMIT_DELETE;
1175 tblk->u.ip = new_ip;
1176 } else if (new_ip->i_nlink == 0) {
1177 assert(!test_cflag(COMMIT_Nolink, new_ip));
1178 /* free block resources */
1179 if ((new_size = commitZeroLink(tid, new_ip)) < 0) {
1180 txAbort(tid, 1); /* Marks FS Dirty */
1181 rc = new_size;
1182 goto out4;
1183 }
1184 tblk = tid_to_tblock(tid);
1185 tblk->xflag |= COMMIT_DELETE;
1186 tblk->u.ip = new_ip;
1187 } else {
1188 new_ip->i_ctime = CURRENT_TIME;
1189 mark_inode_dirty(new_ip);
1190 }
1191 } else {
1192 /*
1193 * Add new directory entry
1194 */
1195 rc = dtSearch(new_dir, &new_dname, &ino, &btstack,
1196 JFS_CREATE);
1197 if (rc) {
1198 jfs_err("jfs_rename didn't expect dtSearch to fail "
1199 "w/rc = %d", rc);
1200 goto out4;
1201 }
1202
1203 ino = old_ip->i_ino;
1204 rc = dtInsert(tid, new_dir, &new_dname, &ino, &btstack);
1205 if (rc) {
1206 if (rc == -EIO)
1207 jfs_err("jfs_rename: dtInsert returned -EIO");
1208 goto out4;
1209 }
1210 if (S_ISDIR(old_ip->i_mode))
1211 new_dir->i_nlink++;
1212 }
1213 /*
1214 * Remove old directory entry
1215 */
1216
1217 ino = old_ip->i_ino;
1218 rc = dtDelete(tid, old_dir, &old_dname, &ino, JFS_REMOVE);
1219 if (rc) {
1220 jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
1221 rc);
1222 txAbort(tid, 1); /* Marks Filesystem dirty */
1223 goto out4;
1224 }
1225 if (S_ISDIR(old_ip->i_mode)) {
1226 old_dir->i_nlink--;
1227 if (old_dir != new_dir) {
1228 /*
1229 * Change inode number of parent for moved directory
1230 */
1231
1232 JFS_IP(old_ip)->i_dtroot.header.idotdot =
1233 cpu_to_le32(new_dir->i_ino);
1234
1235 /* Linelock header of dtree */
1236 tlck = txLock(tid, old_ip,
1237 (struct metapage *) &JFS_IP(old_ip)->bxflag,
1238 tlckDTREE | tlckBTROOT | tlckRELINK);
1239 dtlck = (struct dt_lock *) & tlck->lock;
1240 ASSERT(dtlck->index == 0);
1241 lv = & dtlck->lv[0];
1242 lv->offset = 0;
1243 lv->length = 1;
1244 dtlck->index++;
1245 }
1246 }
1247
1248 /*
1249 * Update ctime on changed/moved inodes & mark dirty
1250 */
1251 old_ip->i_ctime = CURRENT_TIME;
1252 mark_inode_dirty(old_ip);
1253
1254 new_dir->i_ctime = new_dir->i_mtime = current_fs_time(new_dir->i_sb);
1255 mark_inode_dirty(new_dir);
1256
1257 /* Build list of inodes modified by this transaction */
1258 ipcount = 0;
1259 iplist[ipcount++] = old_ip;
1260 if (new_ip)
1261 iplist[ipcount++] = new_ip;
1262 iplist[ipcount++] = old_dir;
1263
1264 if (old_dir != new_dir) {
1265 iplist[ipcount++] = new_dir;
1266 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1267 mark_inode_dirty(old_dir);
1268 }
1269
1270 /*
1271 * Incomplete truncate of file data can
1272 * result in timing problems unless we synchronously commit the
1273 * transaction.
1274 */
1275 if (new_size)
1276 commit_flag = COMMIT_SYNC;
1277 else
1278 commit_flag = 0;
1279
1280 rc = txCommit(tid, ipcount, iplist, commit_flag);
1281
1282 out4:
1283 txEnd(tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 if (new_ip)
Ingo Molnar1de87442006-01-24 15:22:50 -06001285 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -05001286 if (old_dir != new_dir)
1287 mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
1288 mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
1289 mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 while (new_size && (rc == 0)) {
1292 tid = txBegin(new_ip->i_sb, 0);
Ingo Molnar1de87442006-01-24 15:22:50 -06001293 mutex_lock(&JFS_IP(new_ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 new_size = xtTruncate_pmap(tid, new_ip, new_size);
1295 if (new_size < 0) {
1296 txAbort(tid, 1);
1297 rc = new_size;
1298 } else
1299 rc = txCommit(tid, 1, &new_ip, COMMIT_SYNC);
1300 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -06001301 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
1303 if (new_ip && (new_ip->i_nlink == 0))
1304 set_cflag(COMMIT_Nolink, new_ip);
1305 out3:
1306 free_UCSname(&new_dname);
1307 out2:
1308 free_UCSname(&old_dname);
1309 out1:
1310 if (new_ip && !S_ISDIR(new_ip->i_mode))
1311 IWRITE_UNLOCK(new_ip);
1312 /*
1313 * Truncating the directory index table is not guaranteed. It
1314 * may need to be done iteratively
1315 */
1316 if (test_cflag(COMMIT_Stale, old_dir)) {
1317 if (old_dir->i_size > 1)
1318 jfs_truncate_nolock(old_dir, 0);
1319
1320 clear_cflag(COMMIT_Stale, old_dir);
1321 }
1322
1323 jfs_info("jfs_rename: returning %d", rc);
1324 return rc;
1325}
1326
1327
1328/*
1329 * NAME: jfs_mknod
1330 *
1331 * FUNCTION: Create a special file (device)
1332 */
1333static int jfs_mknod(struct inode *dir, struct dentry *dentry,
1334 int mode, dev_t rdev)
1335{
1336 struct jfs_inode_info *jfs_ip;
1337 struct btstack btstack;
1338 struct component_name dname;
1339 ino_t ino;
1340 struct inode *ip;
1341 struct inode *iplist[2];
1342 int rc;
1343 tid_t tid;
1344 struct tblock *tblk;
1345
1346 if (!new_valid_dev(rdev))
1347 return -EINVAL;
1348
1349 jfs_info("jfs_mknod: %s", dentry->d_name.name);
1350
1351 if ((rc = get_UCSname(&dname, dentry)))
1352 goto out;
1353
1354 ip = ialloc(dir, mode);
1355 if (ip == NULL) {
1356 rc = -ENOSPC;
1357 goto out1;
1358 }
1359 jfs_ip = JFS_IP(ip);
1360
1361 tid = txBegin(dir->i_sb, 0);
1362
Ingo Molnar1de87442006-01-24 15:22:50 -06001363 mutex_lock(&JFS_IP(dir)->commit_mutex);
1364 mutex_lock(&JFS_IP(ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001366 rc = jfs_init_acl(tid, ip, dir);
1367 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 goto out3;
1369
Dave Kleikamp1d15b10f2005-09-01 09:05:39 -05001370 rc = jfs_init_security(tid, ip, dir);
1371 if (rc) {
1372 txAbort(tid, 0);
1373 goto out3;
1374 }
1375
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001376 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE))) {
1377 txAbort(tid, 0);
1378 goto out3;
1379 }
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 tblk = tid_to_tblock(tid);
1382 tblk->xflag |= COMMIT_CREATE;
1383 tblk->ino = ip->i_ino;
1384 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
1385
1386 ino = ip->i_ino;
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001387 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack))) {
1388 txAbort(tid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 goto out3;
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 ip->i_op = &jfs_file_inode_operations;
1393 jfs_ip->dev = new_encode_dev(rdev);
1394 init_special_inode(ip, ip->i_mode, rdev);
1395
1396 insert_inode_hash(ip);
1397 mark_inode_dirty(ip);
1398
1399 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1400
1401 mark_inode_dirty(dir);
1402
1403 iplist[0] = dir;
1404 iplist[1] = ip;
1405 rc = txCommit(tid, 2, iplist, 0);
1406
1407 out3:
1408 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -06001409 mutex_unlock(&JFS_IP(ip)->commit_mutex);
1410 mutex_unlock(&JFS_IP(dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 if (rc) {
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001412 free_ea_wmap(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 ip->i_nlink = 0;
1414 iput(ip);
1415 } else
1416 d_instantiate(dentry, ip);
1417
1418 out1:
1419 free_UCSname(&dname);
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 out:
1422 jfs_info("jfs_mknod: returning %d", rc);
1423 return rc;
1424}
1425
1426static struct dentry *jfs_lookup(struct inode *dip, struct dentry *dentry, struct nameidata *nd)
1427{
1428 struct btstack btstack;
1429 ino_t inum;
1430 struct inode *ip;
1431 struct component_name key;
1432 const char *name = dentry->d_name.name;
1433 int len = dentry->d_name.len;
1434 int rc;
1435
1436 jfs_info("jfs_lookup: name = %s", name);
1437
Dave Kleikamp686762c2005-08-17 13:53:13 -05001438 if (JFS_SBI(dip->i_sb)->mntflag & JFS_OS2)
1439 dentry->d_op = &jfs_ci_dentry_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 if ((name[0] == '.') && (len == 1))
1442 inum = dip->i_ino;
1443 else if (strcmp(name, "..") == 0)
1444 inum = PARENT(dip);
1445 else {
1446 if ((rc = get_UCSname(&key, dentry)))
1447 return ERR_PTR(rc);
1448 rc = dtSearch(dip, &key, &inum, &btstack, JFS_LOOKUP);
1449 free_UCSname(&key);
1450 if (rc == -ENOENT) {
1451 d_add(dentry, NULL);
1452 return ERR_PTR(0);
1453 } else if (rc) {
1454 jfs_err("jfs_lookup: dtSearch returned %d", rc);
1455 return ERR_PTR(rc);
1456 }
1457 }
1458
1459 ip = iget(dip->i_sb, inum);
1460 if (ip == NULL || is_bad_inode(ip)) {
1461 jfs_err("jfs_lookup: iget failed on inum %d", (uint) inum);
1462 if (ip)
1463 iput(ip);
1464 return ERR_PTR(-EACCES);
1465 }
1466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 dentry = d_splice_alias(ip, dentry);
1468
1469 if (dentry && (JFS_SBI(dip->i_sb)->mntflag & JFS_OS2))
1470 dentry->d_op = &jfs_ci_dentry_operations;
1471
1472 return dentry;
1473}
1474
1475struct dentry *jfs_get_parent(struct dentry *dentry)
1476{
1477 struct super_block *sb = dentry->d_inode->i_sb;
1478 struct dentry *parent = ERR_PTR(-ENOENT);
1479 struct inode *inode;
1480 unsigned long parent_ino;
1481
1482 parent_ino =
1483 le32_to_cpu(JFS_IP(dentry->d_inode)->i_dtroot.header.idotdot);
1484 inode = iget(sb, parent_ino);
1485 if (inode) {
1486 if (is_bad_inode(inode)) {
1487 iput(inode);
1488 parent = ERR_PTR(-EACCES);
1489 } else {
1490 parent = d_alloc_anon(inode);
1491 if (!parent) {
1492 parent = ERR_PTR(-ENOMEM);
1493 iput(inode);
1494 }
1495 }
1496 }
1497
1498 return parent;
1499}
1500
1501struct inode_operations jfs_dir_inode_operations = {
1502 .create = jfs_create,
1503 .lookup = jfs_lookup,
1504 .link = jfs_link,
1505 .unlink = jfs_unlink,
1506 .symlink = jfs_symlink,
1507 .mkdir = jfs_mkdir,
1508 .rmdir = jfs_rmdir,
1509 .mknod = jfs_mknod,
1510 .rename = jfs_rename,
1511 .setxattr = jfs_setxattr,
1512 .getxattr = jfs_getxattr,
1513 .listxattr = jfs_listxattr,
1514 .removexattr = jfs_removexattr,
1515#ifdef CONFIG_JFS_POSIX_ACL
1516 .setattr = jfs_setattr,
1517 .permission = jfs_permission,
1518#endif
1519};
1520
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001521const struct file_operations jfs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 .read = generic_read_dir,
1523 .readdir = jfs_readdir,
1524 .fsync = jfs_fsync,
Herbert Poetzlfa3241d2006-02-09 09:09:16 -06001525 .ioctl = jfs_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526};
1527
1528static int jfs_ci_hash(struct dentry *dir, struct qstr *this)
1529{
1530 unsigned long hash;
1531 int i;
1532
1533 hash = init_name_hash();
1534 for (i=0; i < this->len; i++)
1535 hash = partial_name_hash(tolower(this->name[i]), hash);
1536 this->hash = end_name_hash(hash);
1537
1538 return 0;
1539}
1540
1541static int jfs_ci_compare(struct dentry *dir, struct qstr *a, struct qstr *b)
1542{
1543 int i, result = 1;
1544
1545 if (a->len != b->len)
1546 goto out;
1547 for (i=0; i < a->len; i++) {
1548 if (tolower(a->name[i]) != tolower(b->name[i]))
1549 goto out;
1550 }
1551 result = 0;
1552
1553 /*
1554 * We want creates to preserve case. A negative dentry, a, that
1555 * has a different case than b may cause a new entry to be created
1556 * with the wrong case. Since we can't tell if a comes from a negative
1557 * dentry, we blindly replace it with b. This should be harmless if
1558 * a is not a negative dentry.
1559 */
1560 memcpy((unsigned char *)a->name, b->name, a->len);
1561out:
1562 return result;
1563}
1564
1565struct dentry_operations jfs_ci_dentry_operations =
1566{
1567 .d_hash = jfs_ci_hash,
1568 .d_compare = jfs_ci_compare,
1569};