blob: a5ac97b9a933afcc8e91673b599f149e92184f16 [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>
Nick Piggin2bc334d2011-01-07 17:49:25 +110021#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/ctype.h>
23#include <linux/quotaops.h>
Christoph Hellwigd425de72007-10-21 16:42:09 -070024#include <linux/exportfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "jfs_incore.h"
26#include "jfs_superblock.h"
27#include "jfs_inode.h"
28#include "jfs_dinode.h"
29#include "jfs_dmap.h"
30#include "jfs_unicode.h"
31#include "jfs_metapage.h"
32#include "jfs_xattr.h"
33#include "jfs_acl.h"
34#include "jfs_debug.h"
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/*
37 * forward references
38 */
Al Viroad28b4e2009-02-20 06:00:49 +000039const struct dentry_operations jfs_ci_dentry_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41static s64 commitZeroLink(tid_t, struct inode *);
42
43/*
Dave Kleikamp4f4b4012005-09-01 09:02:43 -050044 * NAME: free_ea_wmap(inode)
45 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -050046 * FUNCTION: free uncommitted extended attributes from working map
Dave Kleikamp4f4b4012005-09-01 09:02:43 -050047 *
48 */
49static inline void free_ea_wmap(struct inode *inode)
50{
51 dxd_t *ea = &JFS_IP(inode)->ea;
52
53 if (ea->flag & DXD_EXTENT) {
54 /* free EA pages from cache */
55 invalidate_dxd_metapages(inode, *ea);
56 dbFree(inode, addressDXD(ea), lengthDXD(ea));
57 }
58 ea->flag = 0;
59}
60
61/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 * NAME: jfs_create(dip, dentry, mode)
63 *
64 * FUNCTION: create a regular file in the parent directory <dip>
65 * with name = <from dentry> and mode = <mode>
66 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -050067 * PARAMETER: dip - parent directory vnode
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 * dentry - dentry of new file
69 * mode - create mode (rwxrwxrwx).
70 * nd- nd struct
71 *
72 * RETURN: Errors from subroutines
73 *
74 */
Al Viro4acdaf22011-07-26 01:42:34 -040075static int jfs_create(struct inode *dip, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -040076 bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
78 int rc = 0;
79 tid_t tid; /* transaction id */
80 struct inode *ip = NULL; /* child directory inode */
81 ino_t ino;
82 struct component_name dname; /* child directory name */
83 struct btstack btstack;
84 struct inode *iplist[2];
85 struct tblock *tblk;
86
Al Viroa4555892014-10-21 20:11:25 -040087 jfs_info("jfs_create: dip:0x%p name:%pd", dip, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Christoph Hellwig871a2932010-03-03 09:05:07 -050089 dquot_initialize(dip);
Christoph Hellwig907f4552010-03-03 09:05:06 -050090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 /*
92 * search parent directory for entry/freespace
93 * (dtSearch() returns parent directory page pinned)
94 */
95 if ((rc = get_UCSname(&dname, dentry)))
96 goto out1;
97
98 /*
99 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
100 * block there while holding dtree page, so we allocate the inode &
101 * begin the transaction before we search the directory.
102 */
103 ip = ialloc(dip, mode);
Akinobu Mita087387f2006-09-14 09:22:38 -0500104 if (IS_ERR(ip)) {
105 rc = PTR_ERR(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 goto out2;
107 }
108
109 tid = txBegin(dip->i_sb, 0);
110
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600111 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
112 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500114 rc = jfs_init_acl(tid, ip, dip);
115 if (rc)
116 goto out3;
117
Eric Paris2a7dba32011-02-01 11:05:39 -0500118 rc = jfs_init_security(tid, ip, dip, &dentry->d_name);
Dave Kleikamp1d15b10f2005-09-01 09:05:39 -0500119 if (rc) {
120 txAbort(tid, 0);
121 goto out3;
122 }
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
125 jfs_err("jfs_create: dtSearch returned %d", rc);
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500126 txAbort(tid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 goto out3;
128 }
129
130 tblk = tid_to_tblock(tid);
131 tblk->xflag |= COMMIT_CREATE;
132 tblk->ino = ip->i_ino;
133 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
134
135 iplist[0] = dip;
136 iplist[1] = ip;
137
138 /*
139 * initialize the child XAD tree root in-line in inode
140 */
141 xtInitRoot(tid, ip);
142
143 /*
144 * create entry in parent directory for child directory
145 * (dtInsert() releases parent directory page)
146 */
147 ino = ip->i_ino;
148 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
149 if (rc == -EIO) {
150 jfs_err("jfs_create: dtInsert returned -EIO");
151 txAbort(tid, 1); /* Marks Filesystem dirty */
152 } else
153 txAbort(tid, 0); /* Filesystem full */
154 goto out3;
155 }
156
157 ip->i_op = &jfs_file_inode_operations;
158 ip->i_fop = &jfs_file_operations;
159 ip->i_mapping->a_ops = &jfs_aops;
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 mark_inode_dirty(ip);
162
163 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
164
165 mark_inode_dirty(dip);
166
167 rc = txCommit(tid, 2, &iplist[0], 0);
168
169 out3:
170 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600171 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500172 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (rc) {
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500174 free_ea_wmap(ip);
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200175 clear_nlink(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -0600176 unlock_new_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 iput(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -0600178 } else {
Dave Kleikamp1f3403f2008-12-30 22:08:37 -0600179 unlock_new_inode(ip);
Al Viro8fc37ec2012-07-19 09:18:15 +0400180 d_instantiate(dentry, ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -0600181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 out2:
184 free_UCSname(&dname);
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 out1:
187
188 jfs_info("jfs_create: rc:%d", rc);
189 return rc;
190}
191
192
193/*
194 * NAME: jfs_mkdir(dip, dentry, mode)
195 *
196 * FUNCTION: create a child directory in the parent directory <dip>
197 * with name = <from dentry> and mode = <mode>
198 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500199 * PARAMETER: dip - parent directory vnode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 * dentry - dentry of child directory
201 * mode - create mode (rwxrwxrwx).
202 *
203 * RETURN: Errors from subroutines
204 *
205 * note:
206 * EACCESS: user needs search+write permission on the parent directory
207 */
Al Viro18bb1db2011-07-26 01:41:39 -0400208static int jfs_mkdir(struct inode *dip, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 int rc = 0;
211 tid_t tid; /* transaction id */
212 struct inode *ip = NULL; /* child directory inode */
213 ino_t ino;
214 struct component_name dname; /* child directory name */
215 struct btstack btstack;
216 struct inode *iplist[2];
217 struct tblock *tblk;
218
Al Viroa4555892014-10-21 20:11:25 -0400219 jfs_info("jfs_mkdir: dip:0x%p name:%pd", dip, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Christoph Hellwig871a2932010-03-03 09:05:07 -0500221 dquot_initialize(dip);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 /*
224 * search parent directory for entry/freespace
225 * (dtSearch() returns parent directory page pinned)
226 */
227 if ((rc = get_UCSname(&dname, dentry)))
228 goto out1;
229
230 /*
231 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
232 * block there while holding dtree page, so we allocate the inode &
233 * begin the transaction before we search the directory.
234 */
235 ip = ialloc(dip, S_IFDIR | mode);
Akinobu Mita087387f2006-09-14 09:22:38 -0500236 if (IS_ERR(ip)) {
237 rc = PTR_ERR(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 goto out2;
239 }
240
241 tid = txBegin(dip->i_sb, 0);
242
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600243 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
244 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500246 rc = jfs_init_acl(tid, ip, dip);
247 if (rc)
248 goto out3;
249
Eric Paris2a7dba32011-02-01 11:05:39 -0500250 rc = jfs_init_security(tid, ip, dip, &dentry->d_name);
Dave Kleikamp1d15b10f2005-09-01 09:05:39 -0500251 if (rc) {
252 txAbort(tid, 0);
253 goto out3;
254 }
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
257 jfs_err("jfs_mkdir: dtSearch returned %d", rc);
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500258 txAbort(tid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 goto out3;
260 }
261
262 tblk = tid_to_tblock(tid);
263 tblk->xflag |= COMMIT_CREATE;
264 tblk->ino = ip->i_ino;
265 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
266
267 iplist[0] = dip;
268 iplist[1] = ip;
269
270 /*
271 * initialize the child directory in-line in inode
272 */
273 dtInitRoot(tid, ip, dip->i_ino);
274
275 /*
276 * create entry in parent directory for child directory
277 * (dtInsert() releases parent directory page)
278 */
279 ino = ip->i_ino;
280 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
281 if (rc == -EIO) {
282 jfs_err("jfs_mkdir: dtInsert returned -EIO");
283 txAbort(tid, 1); /* Marks Filesystem dirty */
284 } else
285 txAbort(tid, 0); /* Filesystem full */
286 goto out3;
287 }
288
Miklos Szeredibfe86842011-10-28 14:13:29 +0200289 set_nlink(ip, 2); /* for '.' */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 ip->i_op = &jfs_dir_inode_operations;
291 ip->i_fop = &jfs_dir_operations;
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 mark_inode_dirty(ip);
294
295 /* update parent directory inode */
Dave Hansend8c76e62006-09-30 23:29:04 -0700296 inc_nlink(dip); /* for '..' from child directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
298 mark_inode_dirty(dip);
299
300 rc = txCommit(tid, 2, &iplist[0], 0);
301
302 out3:
303 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600304 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500305 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (rc) {
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500307 free_ea_wmap(ip);
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200308 clear_nlink(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -0600309 unlock_new_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 iput(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -0600311 } else {
Dave Kleikamp1f3403f2008-12-30 22:08:37 -0600312 unlock_new_inode(ip);
Al Viro8fc37ec2012-07-19 09:18:15 +0400313 d_instantiate(dentry, ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -0600314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 out2:
317 free_UCSname(&dname);
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 out1:
321
322 jfs_info("jfs_mkdir: rc:%d", rc);
323 return rc;
324}
325
326/*
327 * NAME: jfs_rmdir(dip, dentry)
328 *
329 * FUNCTION: remove a link to child directory
330 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500331 * PARAMETER: dip - parent inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 * dentry - child directory dentry
333 *
334 * RETURN: -EINVAL - if name is . or ..
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500335 * -EINVAL - if . or .. exist but are invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 * errors from subroutines
337 *
338 * note:
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500339 * if other threads have the directory open when the last link
340 * is removed, the "." and ".." entries, if present, are removed before
341 * rmdir() returns and no new entries may be created in the directory,
342 * but the directory is not removed until the last reference to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 * the directory is released (cf.unlink() of regular file).
344 */
345static int jfs_rmdir(struct inode *dip, struct dentry *dentry)
346{
347 int rc;
348 tid_t tid; /* transaction id */
David Howells2b0143b2015-03-17 22:25:59 +0000349 struct inode *ip = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 ino_t ino;
351 struct component_name dname;
352 struct inode *iplist[2];
353 struct tblock *tblk;
354
Al Viroa4555892014-10-21 20:11:25 -0400355 jfs_info("jfs_rmdir: dip:0x%p name:%pd", dip, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 /* Init inode for quota operations. */
Christoph Hellwig871a2932010-03-03 09:05:07 -0500358 dquot_initialize(dip);
359 dquot_initialize(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 /* directory must be empty to be removed */
362 if (!dtEmpty(ip)) {
363 rc = -ENOTEMPTY;
364 goto out;
365 }
366
367 if ((rc = get_UCSname(&dname, dentry))) {
368 goto out;
369 }
370
371 tid = txBegin(dip->i_sb, 0);
372
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600373 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
374 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 iplist[0] = dip;
377 iplist[1] = ip;
378
379 tblk = tid_to_tblock(tid);
380 tblk->xflag |= COMMIT_DELETE;
381 tblk->u.ip = ip;
382
383 /*
384 * delete the entry of target directory from parent directory
385 */
386 ino = ip->i_ino;
387 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
388 jfs_err("jfs_rmdir: dtDelete returned %d", rc);
389 if (rc == -EIO)
390 txAbort(tid, 1);
391 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600392 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500393 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 goto out2;
396 }
397
398 /* update parent directory's link count corresponding
399 * to ".." entry of the target directory deleted
400 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700402 inode_dec_link_count(dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 /*
405 * OS/2 could have created EA and/or ACL
406 */
407 /* free EA from both persistent and working map */
408 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
409 /* free EA pages */
410 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
411 }
412 JFS_IP(ip)->ea.flag = 0;
413
414 /* free ACL from both persistent and working map */
415 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
416 /* free ACL pages */
417 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
418 }
419 JFS_IP(ip)->acl.flag = 0;
420
421 /* mark the target directory as deleted */
Dave Hansence71ec32006-09-30 23:29:06 -0700422 clear_nlink(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 mark_inode_dirty(ip);
424
425 rc = txCommit(tid, 2, &iplist[0], 0);
426
427 txEnd(tid);
428
Ingo Molnar1de87442006-01-24 15:22:50 -0600429 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500430 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 /*
433 * Truncating the directory index table is not guaranteed. It
434 * may need to be done iteratively
435 */
436 if (test_cflag(COMMIT_Stale, dip)) {
437 if (dip->i_size > 1)
438 jfs_truncate_nolock(dip, 0);
439
440 clear_cflag(COMMIT_Stale, dip);
441 }
442
443 out2:
444 free_UCSname(&dname);
445
446 out:
447 jfs_info("jfs_rmdir: rc:%d", rc);
448 return rc;
449}
450
451/*
452 * NAME: jfs_unlink(dip, dentry)
453 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500454 * FUNCTION: remove a link to object <vp> named by <name>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 * from parent directory <dvp>
456 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500457 * PARAMETER: dip - inode of parent directory
458 * dentry - dentry of object to be removed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 *
460 * RETURN: errors from subroutines
461 *
462 * note:
463 * temporary file: if one or more processes have the file open
464 * when the last link is removed, the link will be removed before
465 * unlink() returns, but the removal of the file contents will be
466 * postponed until all references to the files are closed.
467 *
468 * JFS does NOT support unlink() on directories.
469 *
470 */
471static int jfs_unlink(struct inode *dip, struct dentry *dentry)
472{
473 int rc;
474 tid_t tid; /* transaction id */
David Howells2b0143b2015-03-17 22:25:59 +0000475 struct inode *ip = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 ino_t ino;
477 struct component_name dname; /* object name */
478 struct inode *iplist[2];
479 struct tblock *tblk;
480 s64 new_size = 0;
481 int commit_flag;
482
Al Viroa4555892014-10-21 20:11:25 -0400483 jfs_info("jfs_unlink: dip:0x%p name:%pd", dip, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 /* Init inode for quota operations. */
Christoph Hellwig871a2932010-03-03 09:05:07 -0500486 dquot_initialize(dip);
487 dquot_initialize(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 if ((rc = get_UCSname(&dname, dentry)))
490 goto out;
491
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600492 IWRITE_LOCK(ip, RDWRLOCK_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 tid = txBegin(dip->i_sb, 0);
495
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600496 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
497 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 iplist[0] = dip;
500 iplist[1] = ip;
501
502 /*
503 * delete the entry of target file from parent directory
504 */
505 ino = ip->i_ino;
506 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
507 jfs_err("jfs_unlink: dtDelete returned %d", rc);
508 if (rc == -EIO)
509 txAbort(tid, 1); /* Marks FS Dirty */
510 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600511 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500512 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 IWRITE_UNLOCK(ip);
514 goto out1;
515 }
516
517 ASSERT(ip->i_nlink);
518
519 ip->i_ctime = dip->i_ctime = dip->i_mtime = CURRENT_TIME;
520 mark_inode_dirty(dip);
521
522 /* update target's inode */
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700523 inode_dec_link_count(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 /*
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500526 * commit zero link count object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 */
528 if (ip->i_nlink == 0) {
529 assert(!test_cflag(COMMIT_Nolink, ip));
530 /* free block resources */
531 if ((new_size = commitZeroLink(tid, ip)) < 0) {
532 txAbort(tid, 1); /* Marks FS Dirty */
533 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600534 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500535 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 IWRITE_UNLOCK(ip);
537 rc = new_size;
538 goto out1;
539 }
540 tblk = tid_to_tblock(tid);
541 tblk->xflag |= COMMIT_DELETE;
542 tblk->u.ip = ip;
543 }
544
545 /*
546 * Incomplete truncate of file data can
547 * result in timing problems unless we synchronously commit the
548 * transaction.
549 */
550 if (new_size)
551 commit_flag = COMMIT_SYNC;
552 else
553 commit_flag = 0;
554
555 /*
556 * If xtTruncate was incomplete, commit synchronously to avoid
557 * timing complications
558 */
559 rc = txCommit(tid, 2, &iplist[0], commit_flag);
560
561 txEnd(tid);
562
Ingo Molnar1de87442006-01-24 15:22:50 -0600563 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500564 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 while (new_size && (rc == 0)) {
567 tid = txBegin(dip->i_sb, 0);
Ingo Molnar1de87442006-01-24 15:22:50 -0600568 mutex_lock(&JFS_IP(ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 new_size = xtTruncate_pmap(tid, ip, new_size);
570 if (new_size < 0) {
571 txAbort(tid, 1); /* Marks FS Dirty */
572 rc = new_size;
573 } else
574 rc = txCommit(tid, 2, &iplist[0], COMMIT_SYNC);
575 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600576 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
578
579 if (ip->i_nlink == 0)
580 set_cflag(COMMIT_Nolink, ip);
581
582 IWRITE_UNLOCK(ip);
583
584 /*
585 * Truncating the directory index table is not guaranteed. It
586 * may need to be done iteratively
587 */
588 if (test_cflag(COMMIT_Stale, dip)) {
589 if (dip->i_size > 1)
590 jfs_truncate_nolock(dip, 0);
591
592 clear_cflag(COMMIT_Stale, dip);
593 }
594
595 out1:
596 free_UCSname(&dname);
597 out:
598 jfs_info("jfs_unlink: rc:%d", rc);
599 return rc;
600}
601
602/*
603 * NAME: commitZeroLink()
604 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500605 * FUNCTION: for non-directory, called by jfs_remove(),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 * truncate a regular file, directory or symbolic
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500607 * link to zero length. return 0 if type is not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 * one of these.
609 *
610 * if the file is currently associated with a VM segment
611 * only permanent disk and inode map resources are freed,
612 * and neither the inode nor indirect blocks are modified
613 * so that the resources can be later freed in the work
614 * map by ctrunc1.
615 * if there is no VM segment on entry, the resources are
616 * freed in both work and permanent map.
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500617 * (? for temporary file - memory object is cached even
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 * after no reference:
619 * reference count > 0 - )
620 *
621 * PARAMETERS: cd - pointer to commit data structure.
622 * current inode is the one to truncate.
623 *
624 * RETURN: Errors from subroutines
625 */
626static s64 commitZeroLink(tid_t tid, struct inode *ip)
627{
628 int filetype;
629 struct tblock *tblk;
630
631 jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid, ip);
632
633 filetype = ip->i_mode & S_IFMT;
634 switch (filetype) {
635 case S_IFREG:
636 break;
637 case S_IFLNK:
638 /* fast symbolic link */
639 if (ip->i_size < IDATASIZE) {
640 ip->i_size = 0;
641 return 0;
642 }
643 break;
644 default:
645 assert(filetype != S_IFDIR);
646 return 0;
647 }
648
649 set_cflag(COMMIT_Freewmap, ip);
650
651 /* mark transaction of block map update type */
652 tblk = tid_to_tblock(tid);
653 tblk->xflag |= COMMIT_PMAP;
654
655 /*
656 * free EA
657 */
658 if (JFS_IP(ip)->ea.flag & DXD_EXTENT)
659 /* acquire maplock on EA to be freed from block map */
660 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
661
662 /*
663 * free ACL
664 */
665 if (JFS_IP(ip)->acl.flag & DXD_EXTENT)
666 /* acquire maplock on EA to be freed from block map */
667 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
668
669 /*
670 * free xtree/data (truncate to zero length):
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500671 * free xtree/data pages from cache if COMMIT_PWMAP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 * free xtree/data blocks from persistent block map, and
673 * free xtree/data blocks from working block map if COMMIT_PWMAP;
674 */
675 if (ip->i_size)
676 return xtTruncate_pmap(tid, ip, 0);
677
678 return 0;
679}
680
681
682/*
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500683 * NAME: jfs_free_zero_link()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500685 * FUNCTION: for non-directory, called by iClose(),
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500686 * free resources of a file from cache and WORKING map
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 * for a file previously committed with zero link count
688 * while associated with a pager object,
689 *
690 * PARAMETER: ip - pointer to inode of file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 */
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500692void jfs_free_zero_link(struct inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 int type;
695
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500696 jfs_info("jfs_free_zero_link: ip = 0x%p", ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 /* return if not reg or symbolic link or if size is
699 * already ok.
700 */
701 type = ip->i_mode & S_IFMT;
702
703 switch (type) {
704 case S_IFREG:
705 break;
706 case S_IFLNK:
707 /* if its contained in inode nothing to do */
708 if (ip->i_size < IDATASIZE)
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500709 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 break;
711 default:
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500712 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
714
715 /*
716 * free EA
717 */
718 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
719 s64 xaddr = addressDXD(&JFS_IP(ip)->ea);
720 int xlen = lengthDXD(&JFS_IP(ip)->ea);
721 struct maplock maplock; /* maplock for COMMIT_WMAP */
722 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
723
724 /* free EA pages from cache */
725 invalidate_dxd_metapages(ip, JFS_IP(ip)->ea);
726
727 /* free EA extent from working block map */
728 maplock.index = 1;
729 pxdlock = (struct pxd_lock *) & maplock;
730 pxdlock->flag = mlckFREEPXD;
731 PXDaddress(&pxdlock->pxd, xaddr);
732 PXDlength(&pxdlock->pxd, xlen);
733 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
734 }
735
736 /*
737 * free ACL
738 */
739 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
740 s64 xaddr = addressDXD(&JFS_IP(ip)->acl);
741 int xlen = lengthDXD(&JFS_IP(ip)->acl);
742 struct maplock maplock; /* maplock for COMMIT_WMAP */
743 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
744
745 invalidate_dxd_metapages(ip, JFS_IP(ip)->acl);
746
747 /* free ACL extent from working block map */
748 maplock.index = 1;
749 pxdlock = (struct pxd_lock *) & maplock;
750 pxdlock->flag = mlckFREEPXD;
751 PXDaddress(&pxdlock->pxd, xaddr);
752 PXDlength(&pxdlock->pxd, xlen);
753 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
754 }
755
756 /*
757 * free xtree/data (truncate to zero length):
758 * free xtree/data pages from cache, and
759 * free xtree/data blocks from working block map;
760 */
761 if (ip->i_size)
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500762 xtTruncate(0, ip, 0, COMMIT_WMAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
765/*
766 * NAME: jfs_link(vp, dvp, name, crp)
767 *
768 * FUNCTION: create a link to <vp> by the name = <name>
769 * in the parent directory <dvp>
770 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500771 * PARAMETER: vp - target object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 * dvp - parent directory of new link
773 * name - name of new link to target object
774 * crp - credential
775 *
776 * RETURN: Errors from subroutines
777 *
778 * note:
779 * JFS does NOT support link() on directories (to prevent circular
780 * path in the directory hierarchy);
781 * EPERM: the target object is a directory, and either the caller
782 * does not have appropriate privileges or the implementation prohibits
783 * using link() on directories [XPG4.2].
784 *
785 * JFS does NOT support links between file systems:
786 * EXDEV: target object and new link are on different file systems and
787 * implementation does not support links between file systems [XPG4.2].
788 */
789static int jfs_link(struct dentry *old_dentry,
790 struct inode *dir, struct dentry *dentry)
791{
792 int rc;
793 tid_t tid;
David Howells2b0143b2015-03-17 22:25:59 +0000794 struct inode *ip = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 ino_t ino;
796 struct component_name dname;
797 struct btstack btstack;
798 struct inode *iplist[2];
799
Al Viroa4555892014-10-21 20:11:25 -0400800 jfs_info("jfs_link: %pd %pd", old_dentry, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Christoph Hellwig871a2932010-03-03 09:05:07 -0500802 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 tid = txBegin(ip->i_sb, 0);
805
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600806 mutex_lock_nested(&JFS_IP(dir)->commit_mutex, COMMIT_MUTEX_PARENT);
807 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 /*
810 * scan parent directory for entry/freespace
811 */
812 if ((rc = get_UCSname(&dname, dentry)))
813 goto out;
814
815 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
816 goto free_dname;
817
818 /*
819 * create entry for new link in parent directory
820 */
821 ino = ip->i_ino;
822 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
823 goto free_dname;
824
825 /* update object inode */
Dave Hansend8c76e62006-09-30 23:29:04 -0700826 inc_nlink(ip); /* for new link */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 ip->i_ctime = CURRENT_TIME;
Dave Kleikamp988a6492005-10-31 16:53:04 -0600828 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 mark_inode_dirty(dir);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400830 ihold(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 iplist[0] = ip;
833 iplist[1] = dir;
834 rc = txCommit(tid, 2, &iplist[0], 0);
835
836 if (rc) {
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200837 drop_nlink(ip); /* never instantiated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 iput(ip);
839 } else
840 d_instantiate(dentry, ip);
841
842 free_dname:
843 free_UCSname(&dname);
844
845 out:
846 txEnd(tid);
847
Ingo Molnar1de87442006-01-24 15:22:50 -0600848 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500849 mutex_unlock(&JFS_IP(dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 jfs_info("jfs_link: rc:%d", rc);
852 return rc;
853}
854
855/*
856 * NAME: jfs_symlink(dip, dentry, name)
857 *
858 * FUNCTION: creates a symbolic link to <symlink> by name <name>
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500859 * in directory <dip>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500861 * PARAMETER: dip - parent directory vnode
862 * dentry - dentry of symbolic link
863 * name - the path name of the existing object
864 * that will be the source of the link
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 *
866 * RETURN: errors from subroutines
867 *
868 * note:
869 * ENAMETOOLONG: pathname resolution of a symbolic link produced
870 * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
871*/
872
873static int jfs_symlink(struct inode *dip, struct dentry *dentry,
874 const char *name)
875{
876 int rc;
877 tid_t tid;
878 ino_t ino = 0;
879 struct component_name dname;
880 int ssize; /* source pathname size */
881 struct btstack btstack;
David Howells2b0143b2015-03-17 22:25:59 +0000882 struct inode *ip = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 s64 xlen = 0;
884 int bmask = 0, xsize;
Dave Kleikamp3c2c2262011-06-20 13:00:27 -0500885 s64 xaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 struct metapage *mp;
887 struct super_block *sb;
888 struct tblock *tblk;
889
890 struct inode *iplist[2];
891
892 jfs_info("jfs_symlink: dip:0x%p name:%s", dip, name);
893
Christoph Hellwig871a2932010-03-03 09:05:07 -0500894 dquot_initialize(dip);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 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);
Akinobu Mita087387f2006-09-14 09:22:38 -0500911 if (IS_ERR(ip)) {
912 rc = PTR_ERR(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 goto out2;
914 }
915
916 tid = txBegin(dip->i_sb, 0);
917
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600918 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
919 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Eric Paris2a7dba32011-02-01 11:05:39 -0500921 rc = jfs_init_security(tid, ip, dip, &dentry->d_name);
Dave Kleikamp1d15b10f2005-09-01 09:05:39 -0500922 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
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500931 * (dir_create() ANDs in the u.u_cmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 * 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) {
Dmitry Monakhovc7f2e1f2010-04-16 08:05:50 -0500946 ip->i_op = &jfs_fast_symlink_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Al Viroad476fe2015-05-02 10:41:20 -0400948 ip->i_link = JFS_IP(ip)->i_inline;
949 memcpy(ip->i_link, name, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 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
Dmitry Monakhovc7f2e1f2010-04-16 08:05:50 -0500968 ip->i_op = &jfs_symlink_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 ip->i_mapping->a_ops = &jfs_aops;
970
971 /*
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500972 * even though the data of symlink object (source
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 * 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 goto out3;
984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 ip->i_size = ssize - 1;
986 while (ssize) {
987 /* This is kind of silly since PATH_MAX == 4K */
988 int copy_size = min(ssize, PSIZE);
989
990 mp = get_metapage(ip, xaddr, PSIZE, 1);
991
992 if (mp == NULL) {
993 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
994 rc = -EIO;
995 txAbort(tid, 0);
996 goto out3;
997 }
998 memcpy(mp->data, name, copy_size);
999 flush_metapage(mp);
1000 ssize -= copy_size;
1001 name += copy_size;
1002 xaddr += JFS_SBI(sb)->nbperpage;
1003 }
1004 }
1005
1006 /*
1007 * create entry for symbolic link in parent directory
1008 */
1009 rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE);
1010 if (rc == 0) {
1011 ino = ip->i_ino;
1012 rc = dtInsert(tid, dip, &dname, &ino, &btstack);
1013 }
1014 if (rc) {
1015 if (xlen)
1016 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
1017 txAbort(tid, 0);
1018 /* discard new inode */
1019 goto out3;
1020 }
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 mark_inode_dirty(ip);
1023
Dave Kleikamp988a6492005-10-31 16:53:04 -06001024 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
1025 mark_inode_dirty(dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 /*
1027 * commit update of parent directory and link object
1028 */
1029
1030 iplist[0] = dip;
1031 iplist[1] = ip;
1032 rc = txCommit(tid, 2, &iplist[0], 0);
1033
1034 out3:
1035 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -06001036 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -05001037 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (rc) {
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001039 free_ea_wmap(ip);
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02001040 clear_nlink(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001041 unlock_new_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 iput(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001043 } else {
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001044 unlock_new_inode(ip);
Al Viro8fc37ec2012-07-19 09:18:15 +04001045 d_instantiate(dentry, ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
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/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001058 * NAME: jfs_rename
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001060 * FUNCTION: rename a file or directory
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 */
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
Al Viroa4555892014-10-21 20:11:25 -04001083 jfs_info("jfs_rename: %pd %pd", old_dentry, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Christoph Hellwig871a2932010-03-03 09:05:07 -05001085 dquot_initialize(old_dir);
1086 dquot_initialize(new_dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05001087
David Howells2b0143b2015-03-17 22:25:59 +00001088 old_ip = d_inode(old_dentry);
1089 new_ip = d_inode(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 if ((rc = get_UCSname(&old_dname, old_dentry)))
1092 goto out1;
1093
1094 if ((rc = get_UCSname(&new_dname, new_dentry)))
1095 goto out2;
1096
1097 /*
1098 * Make sure source inode number is what we think it is
1099 */
1100 rc = dtSearch(old_dir, &old_dname, &ino, &btstack, JFS_LOOKUP);
1101 if (rc || (ino != old_ip->i_ino)) {
1102 rc = -ENOENT;
1103 goto out3;
1104 }
1105
1106 /*
1107 * Make sure dest inode number (if any) is what we think it is
1108 */
1109 rc = dtSearch(new_dir, &new_dname, &ino, &btstack, JFS_LOOKUP);
Joe Perches09aaa742007-11-13 22:16:08 -06001110 if (!rc) {
Dave Kleikampda8a41d2007-11-13 22:25:41 -06001111 if ((!new_ip) || (ino != new_ip->i_ino)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 rc = -ESTALE;
1113 goto out3;
1114 }
1115 } else if (rc != -ENOENT)
1116 goto out3;
1117 else if (new_ip) {
1118 /* no entry exists, but one was expected */
1119 rc = -ESTALE;
1120 goto out3;
1121 }
1122
1123 if (S_ISDIR(old_ip->i_mode)) {
1124 if (new_ip) {
1125 if (!dtEmpty(new_ip)) {
1126 rc = -ENOTEMPTY;
1127 goto out3;
1128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
1130 } else if (new_ip) {
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001131 IWRITE_LOCK(new_ip, RDWRLOCK_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 /* Init inode for quota operations. */
Christoph Hellwig871a2932010-03-03 09:05:07 -05001133 dquot_initialize(new_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
1135
1136 /*
1137 * The real work starts here
1138 */
1139 tid = txBegin(new_dir->i_sb, 0);
1140
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001141 /*
1142 * How do we know the locking is safe from deadlocks?
1143 * The vfs does the hard part for us. Any time we are taking nested
1144 * commit_mutexes, the vfs already has i_mutex held on the parent.
1145 * Here, the vfs has already taken i_mutex on both old_dir and new_dir.
1146 */
1147 mutex_lock_nested(&JFS_IP(new_dir)->commit_mutex, COMMIT_MUTEX_PARENT);
1148 mutex_lock_nested(&JFS_IP(old_ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 if (old_dir != new_dir)
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001150 mutex_lock_nested(&JFS_IP(old_dir)->commit_mutex,
1151 COMMIT_MUTEX_SECOND_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
1153 if (new_ip) {
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001154 mutex_lock_nested(&JFS_IP(new_ip)->commit_mutex,
1155 COMMIT_MUTEX_VICTIM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 /*
1157 * Change existing directory entry to new inode number
1158 */
1159 ino = new_ip->i_ino;
1160 rc = dtModify(tid, new_dir, &new_dname, &ino,
1161 old_ip->i_ino, JFS_RENAME);
1162 if (rc)
Dave Kleikamp26456952015-07-15 12:52:47 -05001163 goto out_tx;
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001164 drop_nlink(new_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if (S_ISDIR(new_ip->i_mode)) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001166 drop_nlink(new_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (new_ip->i_nlink) {
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -05001168 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (old_dir != new_dir)
Ingo Molnar1de87442006-01-24 15:22:50 -06001170 mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -05001171 mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
1172 mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if (!S_ISDIR(old_ip->i_mode) && new_ip)
1174 IWRITE_UNLOCK(new_ip);
1175 jfs_error(new_ip->i_sb,
Joe Percheseb8630d2013-06-04 16:39:15 -07001176 "new_ip->i_nlink != 0\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 return -EIO;
1178 }
1179 tblk = tid_to_tblock(tid);
1180 tblk->xflag |= COMMIT_DELETE;
1181 tblk->u.ip = new_ip;
1182 } else if (new_ip->i_nlink == 0) {
1183 assert(!test_cflag(COMMIT_Nolink, new_ip));
1184 /* free block resources */
1185 if ((new_size = commitZeroLink(tid, new_ip)) < 0) {
1186 txAbort(tid, 1); /* Marks FS Dirty */
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001187 rc = new_size;
Dave Kleikamp26456952015-07-15 12:52:47 -05001188 goto out_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 }
1190 tblk = tid_to_tblock(tid);
1191 tblk->xflag |= COMMIT_DELETE;
1192 tblk->u.ip = new_ip;
1193 } else {
1194 new_ip->i_ctime = CURRENT_TIME;
1195 mark_inode_dirty(new_ip);
1196 }
1197 } else {
1198 /*
1199 * Add new directory entry
1200 */
1201 rc = dtSearch(new_dir, &new_dname, &ino, &btstack,
1202 JFS_CREATE);
1203 if (rc) {
1204 jfs_err("jfs_rename didn't expect dtSearch to fail "
1205 "w/rc = %d", rc);
Dave Kleikamp26456952015-07-15 12:52:47 -05001206 goto out_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 }
1208
1209 ino = old_ip->i_ino;
1210 rc = dtInsert(tid, new_dir, &new_dname, &ino, &btstack);
1211 if (rc) {
1212 if (rc == -EIO)
1213 jfs_err("jfs_rename: dtInsert returned -EIO");
Dave Kleikamp26456952015-07-15 12:52:47 -05001214 goto out_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
1216 if (S_ISDIR(old_ip->i_mode))
Dave Hansend8c76e62006-09-30 23:29:04 -07001217 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
1219 /*
1220 * Remove old directory entry
1221 */
1222
1223 ino = old_ip->i_ino;
1224 rc = dtDelete(tid, old_dir, &old_dname, &ino, JFS_REMOVE);
1225 if (rc) {
1226 jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
1227 rc);
1228 txAbort(tid, 1); /* Marks Filesystem dirty */
Dave Kleikamp26456952015-07-15 12:52:47 -05001229 goto out_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
1231 if (S_ISDIR(old_ip->i_mode)) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001232 drop_nlink(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (old_dir != new_dir) {
1234 /*
1235 * Change inode number of parent for moved directory
1236 */
1237
1238 JFS_IP(old_ip)->i_dtroot.header.idotdot =
1239 cpu_to_le32(new_dir->i_ino);
1240
1241 /* Linelock header of dtree */
1242 tlck = txLock(tid, old_ip,
1243 (struct metapage *) &JFS_IP(old_ip)->bxflag,
1244 tlckDTREE | tlckBTROOT | tlckRELINK);
1245 dtlck = (struct dt_lock *) & tlck->lock;
1246 ASSERT(dtlck->index == 0);
1247 lv = & dtlck->lv[0];
1248 lv->offset = 0;
1249 lv->length = 1;
1250 dtlck->index++;
1251 }
1252 }
1253
1254 /*
1255 * Update ctime on changed/moved inodes & mark dirty
1256 */
1257 old_ip->i_ctime = CURRENT_TIME;
1258 mark_inode_dirty(old_ip);
1259
1260 new_dir->i_ctime = new_dir->i_mtime = current_fs_time(new_dir->i_sb);
1261 mark_inode_dirty(new_dir);
1262
1263 /* Build list of inodes modified by this transaction */
1264 ipcount = 0;
1265 iplist[ipcount++] = old_ip;
1266 if (new_ip)
1267 iplist[ipcount++] = new_ip;
1268 iplist[ipcount++] = old_dir;
1269
1270 if (old_dir != new_dir) {
1271 iplist[ipcount++] = new_dir;
1272 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1273 mark_inode_dirty(old_dir);
1274 }
1275
1276 /*
1277 * Incomplete truncate of file data can
1278 * result in timing problems unless we synchronously commit the
1279 * transaction.
1280 */
1281 if (new_size)
1282 commit_flag = COMMIT_SYNC;
1283 else
1284 commit_flag = 0;
1285
1286 rc = txCommit(tid, ipcount, iplist, commit_flag);
1287
Dave Kleikamp26456952015-07-15 12:52:47 -05001288 out_tx:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 txEnd(tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if (new_ip)
Ingo Molnar1de87442006-01-24 15:22:50 -06001291 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -05001292 if (old_dir != new_dir)
1293 mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
1294 mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
1295 mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 while (new_size && (rc == 0)) {
1298 tid = txBegin(new_ip->i_sb, 0);
Ingo Molnar1de87442006-01-24 15:22:50 -06001299 mutex_lock(&JFS_IP(new_ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 new_size = xtTruncate_pmap(tid, new_ip, new_size);
1301 if (new_size < 0) {
1302 txAbort(tid, 1);
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001303 rc = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 } else
1305 rc = txCommit(tid, 1, &new_ip, COMMIT_SYNC);
1306 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -06001307 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
1309 if (new_ip && (new_ip->i_nlink == 0))
1310 set_cflag(COMMIT_Nolink, new_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 /*
1312 * Truncating the directory index table is not guaranteed. It
1313 * may need to be done iteratively
1314 */
1315 if (test_cflag(COMMIT_Stale, old_dir)) {
1316 if (old_dir->i_size > 1)
1317 jfs_truncate_nolock(old_dir, 0);
1318
1319 clear_cflag(COMMIT_Stale, old_dir);
1320 }
Dave Kleikamp26456952015-07-15 12:52:47 -05001321 if (new_ip && !S_ISDIR(new_ip->i_mode))
1322 IWRITE_UNLOCK(new_ip);
1323 out3:
1324 free_UCSname(&new_dname);
1325 out2:
1326 free_UCSname(&old_dname);
1327 out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 jfs_info("jfs_rename: returning %d", rc);
1329 return rc;
1330}
1331
1332
1333/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001334 * NAME: jfs_mknod
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001336 * FUNCTION: Create a special file (device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 */
1338static int jfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04001339 umode_t mode, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
1341 struct jfs_inode_info *jfs_ip;
1342 struct btstack btstack;
1343 struct component_name dname;
1344 ino_t ino;
1345 struct inode *ip;
1346 struct inode *iplist[2];
1347 int rc;
1348 tid_t tid;
1349 struct tblock *tblk;
1350
1351 if (!new_valid_dev(rdev))
1352 return -EINVAL;
1353
Al Viroa4555892014-10-21 20:11:25 -04001354 jfs_info("jfs_mknod: %pd", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Christoph Hellwig871a2932010-03-03 09:05:07 -05001356 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05001357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 if ((rc = get_UCSname(&dname, dentry)))
1359 goto out;
1360
1361 ip = ialloc(dir, mode);
Akinobu Mita087387f2006-09-14 09:22:38 -05001362 if (IS_ERR(ip)) {
1363 rc = PTR_ERR(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 goto out1;
1365 }
1366 jfs_ip = JFS_IP(ip);
1367
1368 tid = txBegin(dir->i_sb, 0);
1369
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001370 mutex_lock_nested(&JFS_IP(dir)->commit_mutex, COMMIT_MUTEX_PARENT);
1371 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001373 rc = jfs_init_acl(tid, ip, dir);
1374 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 goto out3;
1376
Eric Paris2a7dba32011-02-01 11:05:39 -05001377 rc = jfs_init_security(tid, ip, dir, &dentry->d_name);
Dave Kleikamp1d15b10f2005-09-01 09:05:39 -05001378 if (rc) {
1379 txAbort(tid, 0);
1380 goto out3;
1381 }
1382
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001383 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE))) {
1384 txAbort(tid, 0);
1385 goto out3;
1386 }
1387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 tblk = tid_to_tblock(tid);
1389 tblk->xflag |= COMMIT_CREATE;
1390 tblk->ino = ip->i_ino;
1391 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
1392
1393 ino = ip->i_ino;
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001394 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack))) {
1395 txAbort(tid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 goto out3;
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 ip->i_op = &jfs_file_inode_operations;
1400 jfs_ip->dev = new_encode_dev(rdev);
1401 init_special_inode(ip, ip->i_mode, rdev);
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 mark_inode_dirty(ip);
1404
1405 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1406
1407 mark_inode_dirty(dir);
1408
1409 iplist[0] = dir;
1410 iplist[1] = ip;
1411 rc = txCommit(tid, 2, iplist, 0);
1412
1413 out3:
1414 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -06001415 mutex_unlock(&JFS_IP(ip)->commit_mutex);
1416 mutex_unlock(&JFS_IP(dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 if (rc) {
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001418 free_ea_wmap(ip);
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02001419 clear_nlink(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001420 unlock_new_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 iput(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001422 } else {
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001423 unlock_new_inode(ip);
Al Viro8fc37ec2012-07-19 09:18:15 +04001424 d_instantiate(dentry, ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 out1:
1428 free_UCSname(&dname);
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 out:
1431 jfs_info("jfs_mknod: returning %d", rc);
1432 return rc;
1433}
1434
Al Viro00cd8dd2012-06-10 17:13:09 -04001435static struct dentry *jfs_lookup(struct inode *dip, struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
1437 struct btstack btstack;
1438 ino_t inum;
1439 struct inode *ip;
1440 struct component_name key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 int rc;
1442
Al Viroa4555892014-10-21 20:11:25 -04001443 jfs_info("jfs_lookup: name = %pd", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Al Viro79ac5a42011-07-17 10:17:46 -04001445 if ((rc = get_UCSname(&key, dentry)))
1446 return ERR_PTR(rc);
1447 rc = dtSearch(dip, &key, &inum, &btstack, JFS_LOOKUP);
1448 free_UCSname(&key);
1449 if (rc == -ENOENT) {
1450 ip = NULL;
1451 } else if (rc) {
1452 jfs_err("jfs_lookup: dtSearch returned %d", rc);
1453 ip = ERR_PTR(rc);
1454 } else {
1455 ip = jfs_iget(dip->i_sb, inum);
1456 if (IS_ERR(ip))
1457 jfs_err("jfs_lookup: iget failed on inum %d", (uint)inum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
1459
Al Viro94b77bd2010-12-18 10:59:31 -05001460 return d_splice_alias(ip, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461}
1462
Christoph Hellwigd425de72007-10-21 16:42:09 -07001463static struct inode *jfs_nfs_get_inode(struct super_block *sb,
1464 u64 ino, u32 generation)
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001465{
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001466 struct inode *inode;
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001467
1468 if (ino == 0)
1469 return ERR_PTR(-ESTALE);
David Howellseab1df72008-02-07 00:15:43 -08001470 inode = jfs_iget(sb, ino);
1471 if (IS_ERR(inode))
1472 return ERR_CAST(inode);
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001473
David Howellseab1df72008-02-07 00:15:43 -08001474 if (generation && inode->i_generation != generation) {
Christoph Hellwigd425de72007-10-21 16:42:09 -07001475 iput(inode);
1476 return ERR_PTR(-ESTALE);
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001477 }
1478
Christoph Hellwigd425de72007-10-21 16:42:09 -07001479 return inode;
1480}
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001481
Christoph Hellwigd425de72007-10-21 16:42:09 -07001482struct dentry *jfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
1483 int fh_len, int fh_type)
1484{
1485 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1486 jfs_nfs_get_inode);
1487}
1488
1489struct dentry *jfs_fh_to_parent(struct super_block *sb, struct fid *fid,
1490 int fh_len, int fh_type)
1491{
1492 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1493 jfs_nfs_get_inode);
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001494}
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496struct dentry *jfs_get_parent(struct dentry *dentry)
1497{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 unsigned long parent_ino;
1499
1500 parent_ino =
David Howells2b0143b2015-03-17 22:25:59 +00001501 le32_to_cpu(JFS_IP(d_inode(dentry))->i_dtroot.header.idotdot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
David Howells2b0143b2015-03-17 22:25:59 +00001503 return d_obtain_alias(jfs_iget(d_inode(dentry)->i_sb, parent_ino));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504}
1505
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001506const struct inode_operations jfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 .create = jfs_create,
1508 .lookup = jfs_lookup,
1509 .link = jfs_link,
1510 .unlink = jfs_unlink,
1511 .symlink = jfs_symlink,
1512 .mkdir = jfs_mkdir,
1513 .rmdir = jfs_rmdir,
1514 .mknod = jfs_mknod,
1515 .rename = jfs_rename,
1516 .setxattr = jfs_setxattr,
1517 .getxattr = jfs_getxattr,
1518 .listxattr = jfs_listxattr,
1519 .removexattr = jfs_removexattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 .setattr = jfs_setattr,
Christoph Hellwig759bfee2010-03-03 09:05:02 -05001521#ifdef CONFIG_JFS_POSIX_ACL
Christoph Hellwig4e34e712011-07-23 17:37:31 +02001522 .get_acl = jfs_get_acl,
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -08001523 .set_acl = jfs_set_acl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524#endif
1525};
1526
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001527const struct file_operations jfs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 .read = generic_read_dir,
Al Viro070a0eb2013-05-17 17:00:34 -04001529 .iterate = jfs_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 .fsync = jfs_fsync,
Andi Kleenbaab81f2008-01-27 16:58:51 -06001531 .unlocked_ioctl = jfs_ioctl,
Andi Kleenef1fc2f2008-01-27 17:02:02 -06001532#ifdef CONFIG_COMPAT
1533 .compat_ioctl = jfs_compat_ioctl,
1534#endif
Christoph Hellwig3222a3e2008-09-03 21:53:01 +02001535 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536};
1537
Linus Torvaldsda53be12013-05-21 15:22:44 -07001538static int jfs_ci_hash(const struct dentry *dir, struct qstr *this)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
1540 unsigned long hash;
1541 int i;
1542
1543 hash = init_name_hash();
1544 for (i=0; i < this->len; i++)
1545 hash = partial_name_hash(tolower(this->name[i]), hash);
1546 this->hash = end_name_hash(hash);
1547
1548 return 0;
1549}
1550
Linus Torvaldsda53be12013-05-21 15:22:44 -07001551static int jfs_ci_compare(const struct dentry *parent, const struct dentry *dentry,
Nick Piggin621e1552011-01-07 17:49:27 +11001552 unsigned int len, const char *str, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
1554 int i, result = 1;
1555
Nick Piggin621e1552011-01-07 17:49:27 +11001556 if (len != name->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 goto out;
Nick Piggin621e1552011-01-07 17:49:27 +11001558 for (i=0; i < len; i++) {
1559 if (tolower(str[i]) != tolower(name->name[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 goto out;
1561 }
1562 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563out:
1564 return result;
1565}
1566
Al Viro0b728e12012-06-10 16:03:43 -04001567static int jfs_ci_revalidate(struct dentry *dentry, unsigned int flags)
Nick Piggin2bc334d2011-01-07 17:49:25 +11001568{
1569 /*
1570 * This is not negative dentry. Always valid.
1571 *
1572 * Note, rename() to existing directory entry will have ->d_inode,
1573 * and will use existing name which isn't specified name by user.
1574 *
1575 * We may be able to drop this positive dentry here. But dropping
1576 * positive dentry isn't good idea. So it's unsupported like
1577 * rename("filename", "FILENAME") for now.
1578 */
David Howells2b0143b2015-03-17 22:25:59 +00001579 if (d_really_is_positive(dentry))
Nick Piggin2bc334d2011-01-07 17:49:25 +11001580 return 1;
1581
1582 /*
1583 * This may be nfsd (or something), anyway, we can't see the
1584 * intent of this. So, since this can be for creation, drop it.
1585 */
Al Viro0b728e12012-06-10 16:03:43 -04001586 if (!flags)
Nick Piggin2bc334d2011-01-07 17:49:25 +11001587 return 0;
1588
1589 /*
1590 * Drop the negative dentry, in order to make sure to use the
1591 * case sensitive name which is specified by user if this is
1592 * for creation.
1593 */
Al Viro0b728e12012-06-10 16:03:43 -04001594 if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
Al Viro407938e2011-06-25 21:37:18 -04001595 return 0;
Nick Piggin2bc334d2011-01-07 17:49:25 +11001596 return 1;
1597}
1598
Al Viroad28b4e2009-02-20 06:00:49 +00001599const struct dentry_operations jfs_ci_dentry_operations =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
1601 .d_hash = jfs_ci_hash,
1602 .d_compare = jfs_ci_compare,
Nick Piggin2bc334d2011-01-07 17:49:25 +11001603 .d_revalidate = jfs_ci_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604};