blob: a9cf8e8675be8713dc5f9311faa20fefcac85156 [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>
Christoph Hellwigd425de72007-10-21 16:42:09 -070023#include <linux/exportfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "jfs_incore.h"
25#include "jfs_superblock.h"
26#include "jfs_inode.h"
27#include "jfs_dinode.h"
28#include "jfs_dmap.h"
29#include "jfs_unicode.h"
30#include "jfs_metapage.h"
31#include "jfs_xattr.h"
32#include "jfs_acl.h"
33#include "jfs_debug.h"
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/*
36 * forward references
37 */
Al Viroad28b4e2009-02-20 06:00:49 +000038const struct dentry_operations jfs_ci_dentry_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40static s64 commitZeroLink(tid_t, struct inode *);
41
42/*
Dave Kleikamp4f4b4012005-09-01 09:02:43 -050043 * NAME: free_ea_wmap(inode)
44 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -050045 * FUNCTION: free uncommitted extended attributes from working map
Dave Kleikamp4f4b4012005-09-01 09:02:43 -050046 *
47 */
48static inline void free_ea_wmap(struct inode *inode)
49{
50 dxd_t *ea = &JFS_IP(inode)->ea;
51
52 if (ea->flag & DXD_EXTENT) {
53 /* free EA pages from cache */
54 invalidate_dxd_metapages(inode, *ea);
55 dbFree(inode, addressDXD(ea), lengthDXD(ea));
56 }
57 ea->flag = 0;
58}
59
60/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 * NAME: jfs_create(dip, dentry, mode)
62 *
63 * FUNCTION: create a regular file in the parent directory <dip>
64 * with name = <from dentry> and mode = <mode>
65 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -050066 * PARAMETER: dip - parent directory vnode
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 * dentry - dentry of new file
68 * mode - create mode (rwxrwxrwx).
69 * nd- nd struct
70 *
71 * RETURN: Errors from subroutines
72 *
73 */
74static int jfs_create(struct inode *dip, struct dentry *dentry, int mode,
75 struct nameidata *nd)
76{
77 int rc = 0;
78 tid_t tid; /* transaction id */
79 struct inode *ip = NULL; /* child directory inode */
80 ino_t ino;
81 struct component_name dname; /* child directory name */
82 struct btstack btstack;
83 struct inode *iplist[2];
84 struct tblock *tblk;
85
86 jfs_info("jfs_create: dip:0x%p name:%s", dip, dentry->d_name.name);
87
Christoph Hellwig871a2932010-03-03 09:05:07 -050088 dquot_initialize(dip);
Christoph Hellwig907f4552010-03-03 09:05:06 -050089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 /*
91 * search parent directory for entry/freespace
92 * (dtSearch() returns parent directory page pinned)
93 */
94 if ((rc = get_UCSname(&dname, dentry)))
95 goto out1;
96
97 /*
98 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
99 * block there while holding dtree page, so we allocate the inode &
100 * begin the transaction before we search the directory.
101 */
102 ip = ialloc(dip, mode);
Akinobu Mita087387f2006-09-14 09:22:38 -0500103 if (IS_ERR(ip)) {
104 rc = PTR_ERR(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 goto out2;
106 }
107
108 tid = txBegin(dip->i_sb, 0);
109
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600110 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
111 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500113 rc = jfs_init_acl(tid, ip, dip);
114 if (rc)
115 goto out3;
116
Dave Kleikamp1d15b102005-09-01 09:05:39 -0500117 rc = jfs_init_security(tid, ip, dip);
118 if (rc) {
119 txAbort(tid, 0);
120 goto out3;
121 }
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
124 jfs_err("jfs_create: dtSearch returned %d", rc);
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500125 txAbort(tid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 goto out3;
127 }
128
129 tblk = tid_to_tblock(tid);
130 tblk->xflag |= COMMIT_CREATE;
131 tblk->ino = ip->i_ino;
132 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
133
134 iplist[0] = dip;
135 iplist[1] = ip;
136
137 /*
138 * initialize the child XAD tree root in-line in inode
139 */
140 xtInitRoot(tid, ip);
141
142 /*
143 * create entry in parent directory for child directory
144 * (dtInsert() releases parent directory page)
145 */
146 ino = ip->i_ino;
147 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
148 if (rc == -EIO) {
149 jfs_err("jfs_create: dtInsert returned -EIO");
150 txAbort(tid, 1); /* Marks Filesystem dirty */
151 } else
152 txAbort(tid, 0); /* Filesystem full */
153 goto out3;
154 }
155
156 ip->i_op = &jfs_file_inode_operations;
157 ip->i_fop = &jfs_file_operations;
158 ip->i_mapping->a_ops = &jfs_aops;
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 mark_inode_dirty(ip);
161
162 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
163
164 mark_inode_dirty(dip);
165
166 rc = txCommit(tid, 2, &iplist[0], 0);
167
168 out3:
169 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600170 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500171 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (rc) {
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500173 free_ea_wmap(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 ip->i_nlink = 0;
Dave Kleikamp1f3403f2008-12-30 22:08:37 -0600175 unlock_new_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 iput(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -0600177 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 d_instantiate(dentry, ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -0600179 unlock_new_inode(ip);
180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 out2:
183 free_UCSname(&dname);
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 out1:
186
187 jfs_info("jfs_create: rc:%d", rc);
188 return rc;
189}
190
191
192/*
193 * NAME: jfs_mkdir(dip, dentry, mode)
194 *
195 * FUNCTION: create a child directory in the parent directory <dip>
196 * with name = <from dentry> and mode = <mode>
197 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500198 * PARAMETER: dip - parent directory vnode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 * dentry - dentry of child directory
200 * mode - create mode (rwxrwxrwx).
201 *
202 * RETURN: Errors from subroutines
203 *
204 * note:
205 * EACCESS: user needs search+write permission on the parent directory
206 */
207static int jfs_mkdir(struct inode *dip, struct dentry *dentry, int mode)
208{
209 int rc = 0;
210 tid_t tid; /* transaction id */
211 struct inode *ip = NULL; /* child directory inode */
212 ino_t ino;
213 struct component_name dname; /* child directory name */
214 struct btstack btstack;
215 struct inode *iplist[2];
216 struct tblock *tblk;
217
218 jfs_info("jfs_mkdir: dip:0x%p name:%s", dip, dentry->d_name.name);
219
Christoph Hellwig871a2932010-03-03 09:05:07 -0500220 dquot_initialize(dip);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 /* link count overflow on parent directory ? */
223 if (dip->i_nlink == JFS_LINK_MAX) {
224 rc = -EMLINK;
225 goto out1;
226 }
227
228 /*
229 * search parent directory for entry/freespace
230 * (dtSearch() returns parent directory page pinned)
231 */
232 if ((rc = get_UCSname(&dname, dentry)))
233 goto out1;
234
235 /*
236 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
237 * block there while holding dtree page, so we allocate the inode &
238 * begin the transaction before we search the directory.
239 */
240 ip = ialloc(dip, S_IFDIR | mode);
Akinobu Mita087387f2006-09-14 09:22:38 -0500241 if (IS_ERR(ip)) {
242 rc = PTR_ERR(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 goto out2;
244 }
245
246 tid = txBegin(dip->i_sb, 0);
247
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600248 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
249 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500251 rc = jfs_init_acl(tid, ip, dip);
252 if (rc)
253 goto out3;
254
Dave Kleikamp1d15b102005-09-01 09:05:39 -0500255 rc = jfs_init_security(tid, ip, dip);
256 if (rc) {
257 txAbort(tid, 0);
258 goto out3;
259 }
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
262 jfs_err("jfs_mkdir: dtSearch returned %d", rc);
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500263 txAbort(tid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 goto out3;
265 }
266
267 tblk = tid_to_tblock(tid);
268 tblk->xflag |= COMMIT_CREATE;
269 tblk->ino = ip->i_ino;
270 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
271
272 iplist[0] = dip;
273 iplist[1] = ip;
274
275 /*
276 * initialize the child directory in-line in inode
277 */
278 dtInitRoot(tid, ip, dip->i_ino);
279
280 /*
281 * create entry in parent directory for child directory
282 * (dtInsert() releases parent directory page)
283 */
284 ino = ip->i_ino;
285 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
286 if (rc == -EIO) {
287 jfs_err("jfs_mkdir: dtInsert returned -EIO");
288 txAbort(tid, 1); /* Marks Filesystem dirty */
289 } else
290 txAbort(tid, 0); /* Filesystem full */
291 goto out3;
292 }
293
294 ip->i_nlink = 2; /* for '.' */
295 ip->i_op = &jfs_dir_inode_operations;
296 ip->i_fop = &jfs_dir_operations;
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 mark_inode_dirty(ip);
299
300 /* update parent directory inode */
Dave Hansend8c76e62006-09-30 23:29:04 -0700301 inc_nlink(dip); /* for '..' from child directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
303 mark_inode_dirty(dip);
304
305 rc = txCommit(tid, 2, &iplist[0], 0);
306
307 out3:
308 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600309 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500310 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (rc) {
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500312 free_ea_wmap(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 ip->i_nlink = 0;
Dave Kleikamp1f3403f2008-12-30 22:08:37 -0600314 unlock_new_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 iput(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -0600316 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 d_instantiate(dentry, ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -0600318 unlock_new_inode(ip);
319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 out2:
322 free_UCSname(&dname);
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 out1:
326
327 jfs_info("jfs_mkdir: rc:%d", rc);
328 return rc;
329}
330
331/*
332 * NAME: jfs_rmdir(dip, dentry)
333 *
334 * FUNCTION: remove a link to child directory
335 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500336 * PARAMETER: dip - parent inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 * dentry - child directory dentry
338 *
339 * RETURN: -EINVAL - if name is . or ..
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500340 * -EINVAL - if . or .. exist but are invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 * errors from subroutines
342 *
343 * note:
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500344 * if other threads have the directory open when the last link
345 * is removed, the "." and ".." entries, if present, are removed before
346 * rmdir() returns and no new entries may be created in the directory,
347 * but the directory is not removed until the last reference to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 * the directory is released (cf.unlink() of regular file).
349 */
350static int jfs_rmdir(struct inode *dip, struct dentry *dentry)
351{
352 int rc;
353 tid_t tid; /* transaction id */
354 struct inode *ip = dentry->d_inode;
355 ino_t ino;
356 struct component_name dname;
357 struct inode *iplist[2];
358 struct tblock *tblk;
359
360 jfs_info("jfs_rmdir: dip:0x%p name:%s", dip, dentry->d_name.name);
361
362 /* Init inode for quota operations. */
Christoph Hellwig871a2932010-03-03 09:05:07 -0500363 dquot_initialize(dip);
364 dquot_initialize(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 /* directory must be empty to be removed */
367 if (!dtEmpty(ip)) {
368 rc = -ENOTEMPTY;
369 goto out;
370 }
371
372 if ((rc = get_UCSname(&dname, dentry))) {
373 goto out;
374 }
375
376 tid = txBegin(dip->i_sb, 0);
377
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600378 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
379 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 iplist[0] = dip;
382 iplist[1] = ip;
383
384 tblk = tid_to_tblock(tid);
385 tblk->xflag |= COMMIT_DELETE;
386 tblk->u.ip = ip;
387
388 /*
389 * delete the entry of target directory from parent directory
390 */
391 ino = ip->i_ino;
392 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
393 jfs_err("jfs_rmdir: dtDelete returned %d", rc);
394 if (rc == -EIO)
395 txAbort(tid, 1);
396 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600397 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500398 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 goto out2;
401 }
402
403 /* update parent directory's link count corresponding
404 * to ".." entry of the target directory deleted
405 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700407 inode_dec_link_count(dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 /*
410 * OS/2 could have created EA and/or ACL
411 */
412 /* free EA from both persistent and working map */
413 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
414 /* free EA pages */
415 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
416 }
417 JFS_IP(ip)->ea.flag = 0;
418
419 /* free ACL from both persistent and working map */
420 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
421 /* free ACL pages */
422 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
423 }
424 JFS_IP(ip)->acl.flag = 0;
425
426 /* mark the target directory as deleted */
Dave Hansence71ec32006-09-30 23:29:06 -0700427 clear_nlink(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 mark_inode_dirty(ip);
429
430 rc = txCommit(tid, 2, &iplist[0], 0);
431
432 txEnd(tid);
433
Ingo Molnar1de87442006-01-24 15:22:50 -0600434 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500435 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 /*
438 * Truncating the directory index table is not guaranteed. It
439 * may need to be done iteratively
440 */
441 if (test_cflag(COMMIT_Stale, dip)) {
442 if (dip->i_size > 1)
443 jfs_truncate_nolock(dip, 0);
444
445 clear_cflag(COMMIT_Stale, dip);
446 }
447
448 out2:
449 free_UCSname(&dname);
450
451 out:
452 jfs_info("jfs_rmdir: rc:%d", rc);
453 return rc;
454}
455
456/*
457 * NAME: jfs_unlink(dip, dentry)
458 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500459 * FUNCTION: remove a link to object <vp> named by <name>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 * from parent directory <dvp>
461 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500462 * PARAMETER: dip - inode of parent directory
463 * dentry - dentry of object to be removed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 *
465 * RETURN: errors from subroutines
466 *
467 * note:
468 * temporary file: if one or more processes have the file open
469 * when the last link is removed, the link will be removed before
470 * unlink() returns, but the removal of the file contents will be
471 * postponed until all references to the files are closed.
472 *
473 * JFS does NOT support unlink() on directories.
474 *
475 */
476static int jfs_unlink(struct inode *dip, struct dentry *dentry)
477{
478 int rc;
479 tid_t tid; /* transaction id */
480 struct inode *ip = dentry->d_inode;
481 ino_t ino;
482 struct component_name dname; /* object name */
483 struct inode *iplist[2];
484 struct tblock *tblk;
485 s64 new_size = 0;
486 int commit_flag;
487
488 jfs_info("jfs_unlink: dip:0x%p name:%s", dip, dentry->d_name.name);
489
490 /* Init inode for quota operations. */
Christoph Hellwig871a2932010-03-03 09:05:07 -0500491 dquot_initialize(dip);
492 dquot_initialize(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 if ((rc = get_UCSname(&dname, dentry)))
495 goto out;
496
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600497 IWRITE_LOCK(ip, RDWRLOCK_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 tid = txBegin(dip->i_sb, 0);
500
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600501 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
502 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 iplist[0] = dip;
505 iplist[1] = ip;
506
507 /*
508 * delete the entry of target file from parent directory
509 */
510 ino = ip->i_ino;
511 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
512 jfs_err("jfs_unlink: dtDelete returned %d", rc);
513 if (rc == -EIO)
514 txAbort(tid, 1); /* Marks FS Dirty */
515 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600516 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500517 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 IWRITE_UNLOCK(ip);
519 goto out1;
520 }
521
522 ASSERT(ip->i_nlink);
523
524 ip->i_ctime = dip->i_ctime = dip->i_mtime = CURRENT_TIME;
525 mark_inode_dirty(dip);
526
527 /* update target's inode */
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700528 inode_dec_link_count(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 /*
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500531 * commit zero link count object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 */
533 if (ip->i_nlink == 0) {
534 assert(!test_cflag(COMMIT_Nolink, ip));
535 /* free block resources */
536 if ((new_size = commitZeroLink(tid, ip)) < 0) {
537 txAbort(tid, 1); /* Marks FS Dirty */
538 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600539 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500540 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 IWRITE_UNLOCK(ip);
542 rc = new_size;
543 goto out1;
544 }
545 tblk = tid_to_tblock(tid);
546 tblk->xflag |= COMMIT_DELETE;
547 tblk->u.ip = ip;
548 }
549
550 /*
551 * Incomplete truncate of file data can
552 * result in timing problems unless we synchronously commit the
553 * transaction.
554 */
555 if (new_size)
556 commit_flag = COMMIT_SYNC;
557 else
558 commit_flag = 0;
559
560 /*
561 * If xtTruncate was incomplete, commit synchronously to avoid
562 * timing complications
563 */
564 rc = txCommit(tid, 2, &iplist[0], commit_flag);
565
566 txEnd(tid);
567
Ingo Molnar1de87442006-01-24 15:22:50 -0600568 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500569 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 while (new_size && (rc == 0)) {
572 tid = txBegin(dip->i_sb, 0);
Ingo Molnar1de87442006-01-24 15:22:50 -0600573 mutex_lock(&JFS_IP(ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 new_size = xtTruncate_pmap(tid, ip, new_size);
575 if (new_size < 0) {
576 txAbort(tid, 1); /* Marks FS Dirty */
577 rc = new_size;
578 } else
579 rc = txCommit(tid, 2, &iplist[0], COMMIT_SYNC);
580 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600581 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
583
584 if (ip->i_nlink == 0)
585 set_cflag(COMMIT_Nolink, ip);
586
587 IWRITE_UNLOCK(ip);
588
589 /*
590 * Truncating the directory index table is not guaranteed. It
591 * may need to be done iteratively
592 */
593 if (test_cflag(COMMIT_Stale, dip)) {
594 if (dip->i_size > 1)
595 jfs_truncate_nolock(dip, 0);
596
597 clear_cflag(COMMIT_Stale, dip);
598 }
599
600 out1:
601 free_UCSname(&dname);
602 out:
603 jfs_info("jfs_unlink: rc:%d", rc);
604 return rc;
605}
606
607/*
608 * NAME: commitZeroLink()
609 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500610 * FUNCTION: for non-directory, called by jfs_remove(),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 * truncate a regular file, directory or symbolic
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500612 * link to zero length. return 0 if type is not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 * one of these.
614 *
615 * if the file is currently associated with a VM segment
616 * only permanent disk and inode map resources are freed,
617 * and neither the inode nor indirect blocks are modified
618 * so that the resources can be later freed in the work
619 * map by ctrunc1.
620 * if there is no VM segment on entry, the resources are
621 * freed in both work and permanent map.
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500622 * (? for temporary file - memory object is cached even
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 * after no reference:
624 * reference count > 0 - )
625 *
626 * PARAMETERS: cd - pointer to commit data structure.
627 * current inode is the one to truncate.
628 *
629 * RETURN: Errors from subroutines
630 */
631static s64 commitZeroLink(tid_t tid, struct inode *ip)
632{
633 int filetype;
634 struct tblock *tblk;
635
636 jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid, ip);
637
638 filetype = ip->i_mode & S_IFMT;
639 switch (filetype) {
640 case S_IFREG:
641 break;
642 case S_IFLNK:
643 /* fast symbolic link */
644 if (ip->i_size < IDATASIZE) {
645 ip->i_size = 0;
646 return 0;
647 }
648 break;
649 default:
650 assert(filetype != S_IFDIR);
651 return 0;
652 }
653
654 set_cflag(COMMIT_Freewmap, ip);
655
656 /* mark transaction of block map update type */
657 tblk = tid_to_tblock(tid);
658 tblk->xflag |= COMMIT_PMAP;
659
660 /*
661 * free EA
662 */
663 if (JFS_IP(ip)->ea.flag & DXD_EXTENT)
664 /* acquire maplock on EA to be freed from block map */
665 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
666
667 /*
668 * free ACL
669 */
670 if (JFS_IP(ip)->acl.flag & DXD_EXTENT)
671 /* acquire maplock on EA to be freed from block map */
672 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
673
674 /*
675 * free xtree/data (truncate to zero length):
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500676 * free xtree/data pages from cache if COMMIT_PWMAP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 * free xtree/data blocks from persistent block map, and
678 * free xtree/data blocks from working block map if COMMIT_PWMAP;
679 */
680 if (ip->i_size)
681 return xtTruncate_pmap(tid, ip, 0);
682
683 return 0;
684}
685
686
687/*
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500688 * NAME: jfs_free_zero_link()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500690 * FUNCTION: for non-directory, called by iClose(),
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500691 * free resources of a file from cache and WORKING map
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 * for a file previously committed with zero link count
693 * while associated with a pager object,
694 *
695 * PARAMETER: ip - pointer to inode of file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 */
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500697void jfs_free_zero_link(struct inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 int type;
700
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500701 jfs_info("jfs_free_zero_link: ip = 0x%p", ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 /* return if not reg or symbolic link or if size is
704 * already ok.
705 */
706 type = ip->i_mode & S_IFMT;
707
708 switch (type) {
709 case S_IFREG:
710 break;
711 case S_IFLNK:
712 /* if its contained in inode nothing to do */
713 if (ip->i_size < IDATASIZE)
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500714 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 break;
716 default:
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500717 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
719
720 /*
721 * free EA
722 */
723 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
724 s64 xaddr = addressDXD(&JFS_IP(ip)->ea);
725 int xlen = lengthDXD(&JFS_IP(ip)->ea);
726 struct maplock maplock; /* maplock for COMMIT_WMAP */
727 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
728
729 /* free EA pages from cache */
730 invalidate_dxd_metapages(ip, JFS_IP(ip)->ea);
731
732 /* free EA extent from working block map */
733 maplock.index = 1;
734 pxdlock = (struct pxd_lock *) & maplock;
735 pxdlock->flag = mlckFREEPXD;
736 PXDaddress(&pxdlock->pxd, xaddr);
737 PXDlength(&pxdlock->pxd, xlen);
738 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
739 }
740
741 /*
742 * free ACL
743 */
744 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
745 s64 xaddr = addressDXD(&JFS_IP(ip)->acl);
746 int xlen = lengthDXD(&JFS_IP(ip)->acl);
747 struct maplock maplock; /* maplock for COMMIT_WMAP */
748 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
749
750 invalidate_dxd_metapages(ip, JFS_IP(ip)->acl);
751
752 /* free ACL extent from working block map */
753 maplock.index = 1;
754 pxdlock = (struct pxd_lock *) & maplock;
755 pxdlock->flag = mlckFREEPXD;
756 PXDaddress(&pxdlock->pxd, xaddr);
757 PXDlength(&pxdlock->pxd, xlen);
758 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
759 }
760
761 /*
762 * free xtree/data (truncate to zero length):
763 * free xtree/data pages from cache, and
764 * free xtree/data blocks from working block map;
765 */
766 if (ip->i_size)
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500767 xtTruncate(0, ip, 0, COMMIT_WMAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
770/*
771 * NAME: jfs_link(vp, dvp, name, crp)
772 *
773 * FUNCTION: create a link to <vp> by the name = <name>
774 * in the parent directory <dvp>
775 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500776 * PARAMETER: vp - target object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 * dvp - parent directory of new link
778 * name - name of new link to target object
779 * crp - credential
780 *
781 * RETURN: Errors from subroutines
782 *
783 * note:
784 * JFS does NOT support link() on directories (to prevent circular
785 * path in the directory hierarchy);
786 * EPERM: the target object is a directory, and either the caller
787 * does not have appropriate privileges or the implementation prohibits
788 * using link() on directories [XPG4.2].
789 *
790 * JFS does NOT support links between file systems:
791 * EXDEV: target object and new link are on different file systems and
792 * implementation does not support links between file systems [XPG4.2].
793 */
794static int jfs_link(struct dentry *old_dentry,
795 struct inode *dir, struct dentry *dentry)
796{
797 int rc;
798 tid_t tid;
799 struct inode *ip = old_dentry->d_inode;
800 ino_t ino;
801 struct component_name dname;
802 struct btstack btstack;
803 struct inode *iplist[2];
804
805 jfs_info("jfs_link: %s %s", old_dentry->d_name.name,
806 dentry->d_name.name);
807
808 if (ip->i_nlink == JFS_LINK_MAX)
809 return -EMLINK;
810
811 if (ip->i_nlink == 0)
812 return -ENOENT;
813
Christoph Hellwig871a2932010-03-03 09:05:07 -0500814 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 tid = txBegin(ip->i_sb, 0);
817
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600818 mutex_lock_nested(&JFS_IP(dir)->commit_mutex, COMMIT_MUTEX_PARENT);
819 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 /*
822 * scan parent directory for entry/freespace
823 */
824 if ((rc = get_UCSname(&dname, dentry)))
825 goto out;
826
827 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
828 goto free_dname;
829
830 /*
831 * create entry for new link in parent directory
832 */
833 ino = ip->i_ino;
834 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
835 goto free_dname;
836
837 /* update object inode */
Dave Hansend8c76e62006-09-30 23:29:04 -0700838 inc_nlink(ip); /* for new link */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 ip->i_ctime = CURRENT_TIME;
Dave Kleikamp988a6492005-10-31 16:53:04 -0600840 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 mark_inode_dirty(dir);
842 atomic_inc(&ip->i_count);
843
844 iplist[0] = ip;
845 iplist[1] = dir;
846 rc = txCommit(tid, 2, &iplist[0], 0);
847
848 if (rc) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700849 ip->i_nlink--; /* never instantiated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 iput(ip);
851 } else
852 d_instantiate(dentry, ip);
853
854 free_dname:
855 free_UCSname(&dname);
856
857 out:
858 txEnd(tid);
859
Ingo Molnar1de87442006-01-24 15:22:50 -0600860 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500861 mutex_unlock(&JFS_IP(dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 jfs_info("jfs_link: rc:%d", rc);
864 return rc;
865}
866
867/*
868 * NAME: jfs_symlink(dip, dentry, name)
869 *
870 * FUNCTION: creates a symbolic link to <symlink> by name <name>
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500871 * in directory <dip>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500873 * PARAMETER: dip - parent directory vnode
874 * dentry - dentry of symbolic link
875 * name - the path name of the existing object
876 * that will be the source of the link
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 *
878 * RETURN: errors from subroutines
879 *
880 * note:
881 * ENAMETOOLONG: pathname resolution of a symbolic link produced
882 * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
883*/
884
885static int jfs_symlink(struct inode *dip, struct dentry *dentry,
886 const char *name)
887{
888 int rc;
889 tid_t tid;
890 ino_t ino = 0;
891 struct component_name dname;
892 int ssize; /* source pathname size */
893 struct btstack btstack;
894 struct inode *ip = dentry->d_inode;
895 unchar *i_fastsymlink;
896 s64 xlen = 0;
897 int bmask = 0, xsize;
898 s64 extent = 0, xaddr;
899 struct metapage *mp;
900 struct super_block *sb;
901 struct tblock *tblk;
902
903 struct inode *iplist[2];
904
905 jfs_info("jfs_symlink: dip:0x%p name:%s", dip, name);
906
Christoph Hellwig871a2932010-03-03 09:05:07 -0500907 dquot_initialize(dip);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 ssize = strlen(name) + 1;
910
911 /*
912 * search parent directory for entry/freespace
913 * (dtSearch() returns parent directory page pinned)
914 */
915
916 if ((rc = get_UCSname(&dname, dentry)))
917 goto out1;
918
919 /*
920 * allocate on-disk/in-memory inode for symbolic link:
921 * (iAlloc() returns new, locked inode)
922 */
923 ip = ialloc(dip, S_IFLNK | 0777);
Akinobu Mita087387f2006-09-14 09:22:38 -0500924 if (IS_ERR(ip)) {
925 rc = PTR_ERR(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 goto out2;
927 }
928
929 tid = txBegin(dip->i_sb, 0);
930
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600931 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
932 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Dave Kleikamp1d15b102005-09-01 09:05:39 -0500934 rc = jfs_init_security(tid, ip, dip);
935 if (rc)
936 goto out3;
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 tblk = tid_to_tblock(tid);
939 tblk->xflag |= COMMIT_CREATE;
940 tblk->ino = ip->i_ino;
941 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
942
943 /* fix symlink access permission
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500944 * (dir_create() ANDs in the u.u_cmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 * but symlinks really need to be 777 access)
946 */
947 ip->i_mode |= 0777;
948
949 /*
950 * write symbolic link target path name
951 */
952 xtInitRoot(tid, ip);
953
954 /*
955 * write source path name inline in on-disk inode (fast symbolic link)
956 */
957
958 if (ssize <= IDATASIZE) {
Dmitry Monakhovc7f2e1f2010-04-16 08:05:50 -0500959 ip->i_op = &jfs_fast_symlink_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 i_fastsymlink = JFS_IP(ip)->i_inline;
962 memcpy(i_fastsymlink, name, ssize);
963 ip->i_size = ssize - 1;
964
965 /*
966 * if symlink is > 128 bytes, we don't have the space to
967 * store inline extended attributes
968 */
969 if (ssize > sizeof (JFS_IP(ip)->i_inline))
970 JFS_IP(ip)->mode2 &= ~INLINEEA;
971
972 jfs_info("jfs_symlink: fast symlink added ssize:%d name:%s ",
973 ssize, name);
974 }
975 /*
976 * write source path name in a single extent
977 */
978 else {
979 jfs_info("jfs_symlink: allocate extent ip:0x%p", ip);
980
Dmitry Monakhovc7f2e1f2010-04-16 08:05:50 -0500981 ip->i_op = &jfs_symlink_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 ip->i_mapping->a_ops = &jfs_aops;
983
984 /*
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500985 * even though the data of symlink object (source
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 * path name) is treated as non-journaled user data,
987 * it is read/written thru buffer cache for performance.
988 */
989 sb = ip->i_sb;
990 bmask = JFS_SBI(sb)->bsize - 1;
991 xsize = (ssize + bmask) & ~bmask;
992 xaddr = 0;
993 xlen = xsize >> JFS_SBI(sb)->l2bsize;
994 if ((rc = xtInsert(tid, ip, 0, 0, xlen, &xaddr, 0))) {
995 txAbort(tid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 goto out3;
997 }
998 extent = xaddr;
999 ip->i_size = ssize - 1;
1000 while (ssize) {
1001 /* This is kind of silly since PATH_MAX == 4K */
1002 int copy_size = min(ssize, PSIZE);
1003
1004 mp = get_metapage(ip, xaddr, PSIZE, 1);
1005
1006 if (mp == NULL) {
1007 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
1008 rc = -EIO;
1009 txAbort(tid, 0);
1010 goto out3;
1011 }
1012 memcpy(mp->data, name, copy_size);
1013 flush_metapage(mp);
1014 ssize -= copy_size;
1015 name += copy_size;
1016 xaddr += JFS_SBI(sb)->nbperpage;
1017 }
1018 }
1019
1020 /*
1021 * create entry for symbolic link in parent directory
1022 */
1023 rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE);
1024 if (rc == 0) {
1025 ino = ip->i_ino;
1026 rc = dtInsert(tid, dip, &dname, &ino, &btstack);
1027 }
1028 if (rc) {
1029 if (xlen)
1030 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
1031 txAbort(tid, 0);
1032 /* discard new inode */
1033 goto out3;
1034 }
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 mark_inode_dirty(ip);
1037
Dave Kleikamp988a6492005-10-31 16:53:04 -06001038 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
1039 mark_inode_dirty(dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 /*
1041 * commit update of parent directory and link object
1042 */
1043
1044 iplist[0] = dip;
1045 iplist[1] = ip;
1046 rc = txCommit(tid, 2, &iplist[0], 0);
1047
1048 out3:
1049 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -06001050 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -05001051 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (rc) {
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001053 free_ea_wmap(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 ip->i_nlink = 0;
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001055 unlock_new_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 iput(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001057 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 d_instantiate(dentry, ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001059 unlock_new_inode(ip);
1060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 out2:
1063 free_UCSname(&dname);
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 out1:
1066 jfs_info("jfs_symlink: rc:%d", rc);
1067 return rc;
1068}
1069
1070
1071/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001072 * NAME: jfs_rename
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001074 * FUNCTION: rename a file or directory
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 */
1076static int jfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1077 struct inode *new_dir, struct dentry *new_dentry)
1078{
1079 struct btstack btstack;
1080 ino_t ino;
1081 struct component_name new_dname;
1082 struct inode *new_ip;
1083 struct component_name old_dname;
1084 struct inode *old_ip;
1085 int rc;
1086 tid_t tid;
1087 struct tlock *tlck;
1088 struct dt_lock *dtlck;
1089 struct lv *lv;
1090 int ipcount;
1091 struct inode *iplist[4];
1092 struct tblock *tblk;
1093 s64 new_size = 0;
1094 int commit_flag;
1095
1096
1097 jfs_info("jfs_rename: %s %s", old_dentry->d_name.name,
1098 new_dentry->d_name.name);
1099
Christoph Hellwig871a2932010-03-03 09:05:07 -05001100 dquot_initialize(old_dir);
1101 dquot_initialize(new_dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05001102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 old_ip = old_dentry->d_inode;
1104 new_ip = new_dentry->d_inode;
1105
1106 if ((rc = get_UCSname(&old_dname, old_dentry)))
1107 goto out1;
1108
1109 if ((rc = get_UCSname(&new_dname, new_dentry)))
1110 goto out2;
1111
1112 /*
1113 * Make sure source inode number is what we think it is
1114 */
1115 rc = dtSearch(old_dir, &old_dname, &ino, &btstack, JFS_LOOKUP);
1116 if (rc || (ino != old_ip->i_ino)) {
1117 rc = -ENOENT;
1118 goto out3;
1119 }
1120
1121 /*
1122 * Make sure dest inode number (if any) is what we think it is
1123 */
1124 rc = dtSearch(new_dir, &new_dname, &ino, &btstack, JFS_LOOKUP);
Joe Perches09aaa742007-11-13 22:16:08 -06001125 if (!rc) {
Dave Kleikampda8a41d2007-11-13 22:25:41 -06001126 if ((!new_ip) || (ino != new_ip->i_ino)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 rc = -ESTALE;
1128 goto out3;
1129 }
1130 } else if (rc != -ENOENT)
1131 goto out3;
1132 else if (new_ip) {
1133 /* no entry exists, but one was expected */
1134 rc = -ESTALE;
1135 goto out3;
1136 }
1137
1138 if (S_ISDIR(old_ip->i_mode)) {
1139 if (new_ip) {
1140 if (!dtEmpty(new_ip)) {
1141 rc = -ENOTEMPTY;
1142 goto out3;
1143 }
1144 } else if ((new_dir != old_dir) &&
1145 (new_dir->i_nlink == JFS_LINK_MAX)) {
1146 rc = -EMLINK;
1147 goto out3;
1148 }
1149 } else if (new_ip) {
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001150 IWRITE_LOCK(new_ip, RDWRLOCK_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 /* Init inode for quota operations. */
Christoph Hellwig871a2932010-03-03 09:05:07 -05001152 dquot_initialize(new_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
1154
1155 /*
1156 * The real work starts here
1157 */
1158 tid = txBegin(new_dir->i_sb, 0);
1159
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001160 /*
1161 * How do we know the locking is safe from deadlocks?
1162 * The vfs does the hard part for us. Any time we are taking nested
1163 * commit_mutexes, the vfs already has i_mutex held on the parent.
1164 * Here, the vfs has already taken i_mutex on both old_dir and new_dir.
1165 */
1166 mutex_lock_nested(&JFS_IP(new_dir)->commit_mutex, COMMIT_MUTEX_PARENT);
1167 mutex_lock_nested(&JFS_IP(old_ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (old_dir != new_dir)
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001169 mutex_lock_nested(&JFS_IP(old_dir)->commit_mutex,
1170 COMMIT_MUTEX_SECOND_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 if (new_ip) {
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001173 mutex_lock_nested(&JFS_IP(new_ip)->commit_mutex,
1174 COMMIT_MUTEX_VICTIM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 /*
1176 * Change existing directory entry to new inode number
1177 */
1178 ino = new_ip->i_ino;
1179 rc = dtModify(tid, new_dir, &new_dname, &ino,
1180 old_ip->i_ino, JFS_RENAME);
1181 if (rc)
1182 goto out4;
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001183 drop_nlink(new_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (S_ISDIR(new_ip->i_mode)) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001185 drop_nlink(new_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if (new_ip->i_nlink) {
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -05001187 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 if (old_dir != new_dir)
Ingo Molnar1de87442006-01-24 15:22:50 -06001189 mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -05001190 mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
1191 mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 if (!S_ISDIR(old_ip->i_mode) && new_ip)
1193 IWRITE_UNLOCK(new_ip);
1194 jfs_error(new_ip->i_sb,
1195 "jfs_rename: new_ip->i_nlink != 0");
1196 return -EIO;
1197 }
1198 tblk = tid_to_tblock(tid);
1199 tblk->xflag |= COMMIT_DELETE;
1200 tblk->u.ip = new_ip;
1201 } else if (new_ip->i_nlink == 0) {
1202 assert(!test_cflag(COMMIT_Nolink, new_ip));
1203 /* free block resources */
1204 if ((new_size = commitZeroLink(tid, new_ip)) < 0) {
1205 txAbort(tid, 1); /* Marks FS Dirty */
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001206 rc = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 goto out4;
1208 }
1209 tblk = tid_to_tblock(tid);
1210 tblk->xflag |= COMMIT_DELETE;
1211 tblk->u.ip = new_ip;
1212 } else {
1213 new_ip->i_ctime = CURRENT_TIME;
1214 mark_inode_dirty(new_ip);
1215 }
1216 } else {
1217 /*
1218 * Add new directory entry
1219 */
1220 rc = dtSearch(new_dir, &new_dname, &ino, &btstack,
1221 JFS_CREATE);
1222 if (rc) {
1223 jfs_err("jfs_rename didn't expect dtSearch to fail "
1224 "w/rc = %d", rc);
1225 goto out4;
1226 }
1227
1228 ino = old_ip->i_ino;
1229 rc = dtInsert(tid, new_dir, &new_dname, &ino, &btstack);
1230 if (rc) {
1231 if (rc == -EIO)
1232 jfs_err("jfs_rename: dtInsert returned -EIO");
1233 goto out4;
1234 }
1235 if (S_ISDIR(old_ip->i_mode))
Dave Hansend8c76e62006-09-30 23:29:04 -07001236 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 }
1238 /*
1239 * Remove old directory entry
1240 */
1241
1242 ino = old_ip->i_ino;
1243 rc = dtDelete(tid, old_dir, &old_dname, &ino, JFS_REMOVE);
1244 if (rc) {
1245 jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
1246 rc);
1247 txAbort(tid, 1); /* Marks Filesystem dirty */
1248 goto out4;
1249 }
1250 if (S_ISDIR(old_ip->i_mode)) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001251 drop_nlink(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if (old_dir != new_dir) {
1253 /*
1254 * Change inode number of parent for moved directory
1255 */
1256
1257 JFS_IP(old_ip)->i_dtroot.header.idotdot =
1258 cpu_to_le32(new_dir->i_ino);
1259
1260 /* Linelock header of dtree */
1261 tlck = txLock(tid, old_ip,
1262 (struct metapage *) &JFS_IP(old_ip)->bxflag,
1263 tlckDTREE | tlckBTROOT | tlckRELINK);
1264 dtlck = (struct dt_lock *) & tlck->lock;
1265 ASSERT(dtlck->index == 0);
1266 lv = & dtlck->lv[0];
1267 lv->offset = 0;
1268 lv->length = 1;
1269 dtlck->index++;
1270 }
1271 }
1272
1273 /*
1274 * Update ctime on changed/moved inodes & mark dirty
1275 */
1276 old_ip->i_ctime = CURRENT_TIME;
1277 mark_inode_dirty(old_ip);
1278
1279 new_dir->i_ctime = new_dir->i_mtime = current_fs_time(new_dir->i_sb);
1280 mark_inode_dirty(new_dir);
1281
1282 /* Build list of inodes modified by this transaction */
1283 ipcount = 0;
1284 iplist[ipcount++] = old_ip;
1285 if (new_ip)
1286 iplist[ipcount++] = new_ip;
1287 iplist[ipcount++] = old_dir;
1288
1289 if (old_dir != new_dir) {
1290 iplist[ipcount++] = new_dir;
1291 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1292 mark_inode_dirty(old_dir);
1293 }
1294
1295 /*
1296 * Incomplete truncate of file data can
1297 * result in timing problems unless we synchronously commit the
1298 * transaction.
1299 */
1300 if (new_size)
1301 commit_flag = COMMIT_SYNC;
1302 else
1303 commit_flag = 0;
1304
1305 rc = txCommit(tid, ipcount, iplist, commit_flag);
1306
1307 out4:
1308 txEnd(tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 if (new_ip)
Ingo Molnar1de87442006-01-24 15:22:50 -06001310 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -05001311 if (old_dir != new_dir)
1312 mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
1313 mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
1314 mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 while (new_size && (rc == 0)) {
1317 tid = txBegin(new_ip->i_sb, 0);
Ingo Molnar1de87442006-01-24 15:22:50 -06001318 mutex_lock(&JFS_IP(new_ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 new_size = xtTruncate_pmap(tid, new_ip, new_size);
1320 if (new_size < 0) {
1321 txAbort(tid, 1);
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001322 rc = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 } else
1324 rc = txCommit(tid, 1, &new_ip, COMMIT_SYNC);
1325 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -06001326 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
1328 if (new_ip && (new_ip->i_nlink == 0))
1329 set_cflag(COMMIT_Nolink, new_ip);
1330 out3:
1331 free_UCSname(&new_dname);
1332 out2:
1333 free_UCSname(&old_dname);
1334 out1:
1335 if (new_ip && !S_ISDIR(new_ip->i_mode))
1336 IWRITE_UNLOCK(new_ip);
1337 /*
1338 * Truncating the directory index table is not guaranteed. It
1339 * may need to be done iteratively
1340 */
1341 if (test_cflag(COMMIT_Stale, old_dir)) {
1342 if (old_dir->i_size > 1)
1343 jfs_truncate_nolock(old_dir, 0);
1344
1345 clear_cflag(COMMIT_Stale, old_dir);
1346 }
1347
1348 jfs_info("jfs_rename: returning %d", rc);
1349 return rc;
1350}
1351
1352
1353/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001354 * NAME: jfs_mknod
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001356 * FUNCTION: Create a special file (device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 */
1358static int jfs_mknod(struct inode *dir, struct dentry *dentry,
1359 int mode, dev_t rdev)
1360{
1361 struct jfs_inode_info *jfs_ip;
1362 struct btstack btstack;
1363 struct component_name dname;
1364 ino_t ino;
1365 struct inode *ip;
1366 struct inode *iplist[2];
1367 int rc;
1368 tid_t tid;
1369 struct tblock *tblk;
1370
1371 if (!new_valid_dev(rdev))
1372 return -EINVAL;
1373
1374 jfs_info("jfs_mknod: %s", dentry->d_name.name);
1375
Christoph Hellwig871a2932010-03-03 09:05:07 -05001376 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05001377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if ((rc = get_UCSname(&dname, dentry)))
1379 goto out;
1380
1381 ip = ialloc(dir, mode);
Akinobu Mita087387f2006-09-14 09:22:38 -05001382 if (IS_ERR(ip)) {
1383 rc = PTR_ERR(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 goto out1;
1385 }
1386 jfs_ip = JFS_IP(ip);
1387
1388 tid = txBegin(dir->i_sb, 0);
1389
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001390 mutex_lock_nested(&JFS_IP(dir)->commit_mutex, COMMIT_MUTEX_PARENT);
1391 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001393 rc = jfs_init_acl(tid, ip, dir);
1394 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 goto out3;
1396
Dave Kleikamp1d15b102005-09-01 09:05:39 -05001397 rc = jfs_init_security(tid, ip, dir);
1398 if (rc) {
1399 txAbort(tid, 0);
1400 goto out3;
1401 }
1402
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001403 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE))) {
1404 txAbort(tid, 0);
1405 goto out3;
1406 }
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 tblk = tid_to_tblock(tid);
1409 tblk->xflag |= COMMIT_CREATE;
1410 tblk->ino = ip->i_ino;
1411 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
1412
1413 ino = ip->i_ino;
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001414 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack))) {
1415 txAbort(tid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 goto out3;
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 ip->i_op = &jfs_file_inode_operations;
1420 jfs_ip->dev = new_encode_dev(rdev);
1421 init_special_inode(ip, ip->i_mode, rdev);
1422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 mark_inode_dirty(ip);
1424
1425 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1426
1427 mark_inode_dirty(dir);
1428
1429 iplist[0] = dir;
1430 iplist[1] = ip;
1431 rc = txCommit(tid, 2, iplist, 0);
1432
1433 out3:
1434 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -06001435 mutex_unlock(&JFS_IP(ip)->commit_mutex);
1436 mutex_unlock(&JFS_IP(dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 if (rc) {
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001438 free_ea_wmap(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 ip->i_nlink = 0;
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001440 unlock_new_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 iput(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001442 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 d_instantiate(dentry, ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001444 unlock_new_inode(ip);
1445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 out1:
1448 free_UCSname(&dname);
1449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 out:
1451 jfs_info("jfs_mknod: returning %d", rc);
1452 return rc;
1453}
1454
1455static struct dentry *jfs_lookup(struct inode *dip, struct dentry *dentry, struct nameidata *nd)
1456{
1457 struct btstack btstack;
1458 ino_t inum;
1459 struct inode *ip;
1460 struct component_name key;
1461 const char *name = dentry->d_name.name;
1462 int len = dentry->d_name.len;
1463 int rc;
1464
1465 jfs_info("jfs_lookup: name = %s", name);
1466
Dave Kleikamp686762c2005-08-17 13:53:13 -05001467 if (JFS_SBI(dip->i_sb)->mntflag & JFS_OS2)
1468 dentry->d_op = &jfs_ci_dentry_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
1470 if ((name[0] == '.') && (len == 1))
1471 inum = dip->i_ino;
1472 else if (strcmp(name, "..") == 0)
1473 inum = PARENT(dip);
1474 else {
1475 if ((rc = get_UCSname(&key, dentry)))
1476 return ERR_PTR(rc);
1477 rc = dtSearch(dip, &key, &inum, &btstack, JFS_LOOKUP);
1478 free_UCSname(&key);
1479 if (rc == -ENOENT) {
1480 d_add(dentry, NULL);
Marcin Slusarz88f85a52008-05-12 16:42:43 -05001481 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 } else if (rc) {
1483 jfs_err("jfs_lookup: dtSearch returned %d", rc);
1484 return ERR_PTR(rc);
1485 }
1486 }
1487
David Howellseab1df72008-02-07 00:15:43 -08001488 ip = jfs_iget(dip->i_sb, inum);
1489 if (IS_ERR(ip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 jfs_err("jfs_lookup: iget failed on inum %d", (uint) inum);
David Howellseab1df72008-02-07 00:15:43 -08001491 return ERR_CAST(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 }
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 dentry = d_splice_alias(ip, dentry);
1495
1496 if (dentry && (JFS_SBI(dip->i_sb)->mntflag & JFS_OS2))
1497 dentry->d_op = &jfs_ci_dentry_operations;
1498
1499 return dentry;
1500}
1501
Christoph Hellwigd425de72007-10-21 16:42:09 -07001502static struct inode *jfs_nfs_get_inode(struct super_block *sb,
1503 u64 ino, u32 generation)
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001504{
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001505 struct inode *inode;
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001506
1507 if (ino == 0)
1508 return ERR_PTR(-ESTALE);
David Howellseab1df72008-02-07 00:15:43 -08001509 inode = jfs_iget(sb, ino);
1510 if (IS_ERR(inode))
1511 return ERR_CAST(inode);
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001512
David Howellseab1df72008-02-07 00:15:43 -08001513 if (generation && inode->i_generation != generation) {
Christoph Hellwigd425de72007-10-21 16:42:09 -07001514 iput(inode);
1515 return ERR_PTR(-ESTALE);
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001516 }
1517
Christoph Hellwigd425de72007-10-21 16:42:09 -07001518 return inode;
1519}
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001520
Christoph Hellwigd425de72007-10-21 16:42:09 -07001521struct dentry *jfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
1522 int fh_len, int fh_type)
1523{
1524 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1525 jfs_nfs_get_inode);
1526}
1527
1528struct dentry *jfs_fh_to_parent(struct super_block *sb, struct fid *fid,
1529 int fh_len, int fh_type)
1530{
1531 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1532 jfs_nfs_get_inode);
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001533}
1534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535struct dentry *jfs_get_parent(struct dentry *dentry)
1536{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 unsigned long parent_ino;
1538
1539 parent_ino =
1540 le32_to_cpu(JFS_IP(dentry->d_inode)->i_dtroot.header.idotdot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Christoph Hellwig44003722008-08-11 15:49:04 +02001542 return d_obtain_alias(jfs_iget(dentry->d_inode->i_sb, parent_ino));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543}
1544
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001545const struct inode_operations jfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 .create = jfs_create,
1547 .lookup = jfs_lookup,
1548 .link = jfs_link,
1549 .unlink = jfs_unlink,
1550 .symlink = jfs_symlink,
1551 .mkdir = jfs_mkdir,
1552 .rmdir = jfs_rmdir,
1553 .mknod = jfs_mknod,
1554 .rename = jfs_rename,
1555 .setxattr = jfs_setxattr,
1556 .getxattr = jfs_getxattr,
1557 .listxattr = jfs_listxattr,
1558 .removexattr = jfs_removexattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 .setattr = jfs_setattr,
Christoph Hellwig759bfee2010-03-03 09:05:02 -05001560#ifdef CONFIG_JFS_POSIX_ACL
Linus Torvalds18f4c642009-08-28 12:29:03 -07001561 .check_acl = jfs_check_acl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562#endif
1563};
1564
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001565const struct file_operations jfs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 .read = generic_read_dir,
1567 .readdir = jfs_readdir,
1568 .fsync = jfs_fsync,
Andi Kleenbaab81f2008-01-27 16:58:51 -06001569 .unlocked_ioctl = jfs_ioctl,
Andi Kleenef1fc2f2008-01-27 17:02:02 -06001570#ifdef CONFIG_COMPAT
1571 .compat_ioctl = jfs_compat_ioctl,
1572#endif
Christoph Hellwig3222a3e2008-09-03 21:53:01 +02001573 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574};
1575
1576static int jfs_ci_hash(struct dentry *dir, struct qstr *this)
1577{
1578 unsigned long hash;
1579 int i;
1580
1581 hash = init_name_hash();
1582 for (i=0; i < this->len; i++)
1583 hash = partial_name_hash(tolower(this->name[i]), hash);
1584 this->hash = end_name_hash(hash);
1585
1586 return 0;
1587}
1588
1589static int jfs_ci_compare(struct dentry *dir, struct qstr *a, struct qstr *b)
1590{
1591 int i, result = 1;
1592
1593 if (a->len != b->len)
1594 goto out;
1595 for (i=0; i < a->len; i++) {
1596 if (tolower(a->name[i]) != tolower(b->name[i]))
1597 goto out;
1598 }
1599 result = 0;
1600
1601 /*
1602 * We want creates to preserve case. A negative dentry, a, that
1603 * has a different case than b may cause a new entry to be created
1604 * with the wrong case. Since we can't tell if a comes from a negative
1605 * dentry, we blindly replace it with b. This should be harmless if
1606 * a is not a negative dentry.
1607 */
1608 memcpy((unsigned char *)a->name, b->name, a->len);
1609out:
1610 return result;
1611}
1612
Al Viroad28b4e2009-02-20 06:00:49 +00001613const struct dentry_operations jfs_ci_dentry_operations =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
1615 .d_hash = jfs_ci_hash,
1616 .d_compare = jfs_ci_compare,
1617};