blob: 7a55d14cc1af0da7fe3fb06eac4fa2c5f117f34c [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Copyright (C) International Business Machines Corp., 2000-2004
4 * Portions Copyright (C) Christoph Hellwig, 2001-2002
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
6
7#include <linux/fs.h>
Nick Piggin2bc334d2011-01-07 17:49:25 +11008#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/ctype.h>
10#include <linux/quotaops.h>
Christoph Hellwigd425de72007-10-21 16:42:09 -070011#include <linux/exportfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "jfs_incore.h"
13#include "jfs_superblock.h"
14#include "jfs_inode.h"
15#include "jfs_dinode.h"
16#include "jfs_dmap.h"
17#include "jfs_unicode.h"
18#include "jfs_metapage.h"
19#include "jfs_xattr.h"
20#include "jfs_acl.h"
21#include "jfs_debug.h"
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023/*
24 * forward references
25 */
Al Viroad28b4e2009-02-20 06:00:49 +000026const struct dentry_operations jfs_ci_dentry_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28static s64 commitZeroLink(tid_t, struct inode *);
29
30/*
Dave Kleikamp4f4b4012005-09-01 09:02:43 -050031 * NAME: free_ea_wmap(inode)
32 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -050033 * FUNCTION: free uncommitted extended attributes from working map
Dave Kleikamp4f4b4012005-09-01 09:02:43 -050034 *
35 */
36static inline void free_ea_wmap(struct inode *inode)
37{
38 dxd_t *ea = &JFS_IP(inode)->ea;
39
40 if (ea->flag & DXD_EXTENT) {
41 /* free EA pages from cache */
42 invalidate_dxd_metapages(inode, *ea);
43 dbFree(inode, addressDXD(ea), lengthDXD(ea));
44 }
45 ea->flag = 0;
46}
47
48/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * NAME: jfs_create(dip, dentry, mode)
50 *
51 * FUNCTION: create a regular file in the parent directory <dip>
52 * with name = <from dentry> and mode = <mode>
53 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -050054 * PARAMETER: dip - parent directory vnode
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * dentry - dentry of new file
56 * mode - create mode (rwxrwxrwx).
57 * nd- nd struct
58 *
59 * RETURN: Errors from subroutines
60 *
61 */
Al Viro4acdaf22011-07-26 01:42:34 -040062static int jfs_create(struct inode *dip, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -040063 bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 int rc = 0;
66 tid_t tid; /* transaction id */
67 struct inode *ip = NULL; /* child directory inode */
68 ino_t ino;
69 struct component_name dname; /* child directory name */
70 struct btstack btstack;
71 struct inode *iplist[2];
72 struct tblock *tblk;
73
Al Viroa4555892014-10-21 20:11:25 -040074 jfs_info("jfs_create: dip:0x%p name:%pd", dip, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Dave Kleikampacc84b02015-07-15 13:53:19 -050076 rc = dquot_initialize(dip);
77 if (rc)
78 goto out1;
Christoph Hellwig907f4552010-03-03 09:05:06 -050079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 /*
81 * search parent directory for entry/freespace
82 * (dtSearch() returns parent directory page pinned)
83 */
84 if ((rc = get_UCSname(&dname, dentry)))
85 goto out1;
86
87 /*
88 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
89 * block there while holding dtree page, so we allocate the inode &
90 * begin the transaction before we search the directory.
91 */
92 ip = ialloc(dip, mode);
Akinobu Mita087387f2006-09-14 09:22:38 -050093 if (IS_ERR(ip)) {
94 rc = PTR_ERR(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 goto out2;
96 }
97
98 tid = txBegin(dip->i_sb, 0);
99
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600100 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
101 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500103 rc = jfs_init_acl(tid, ip, dip);
104 if (rc)
105 goto out3;
106
Eric Paris2a7dba32011-02-01 11:05:39 -0500107 rc = jfs_init_security(tid, ip, dip, &dentry->d_name);
Dave Kleikamp1d15b10f2005-09-01 09:05:39 -0500108 if (rc) {
109 txAbort(tid, 0);
110 goto out3;
111 }
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
114 jfs_err("jfs_create: dtSearch returned %d", rc);
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500115 txAbort(tid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 goto out3;
117 }
118
119 tblk = tid_to_tblock(tid);
120 tblk->xflag |= COMMIT_CREATE;
121 tblk->ino = ip->i_ino;
122 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
123
124 iplist[0] = dip;
125 iplist[1] = ip;
126
127 /*
128 * initialize the child XAD tree root in-line in inode
129 */
130 xtInitRoot(tid, ip);
131
132 /*
133 * create entry in parent directory for child directory
134 * (dtInsert() releases parent directory page)
135 */
136 ino = ip->i_ino;
137 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
138 if (rc == -EIO) {
139 jfs_err("jfs_create: dtInsert returned -EIO");
140 txAbort(tid, 1); /* Marks Filesystem dirty */
141 } else
142 txAbort(tid, 0); /* Filesystem full */
143 goto out3;
144 }
145
146 ip->i_op = &jfs_file_inode_operations;
147 ip->i_fop = &jfs_file_operations;
148 ip->i_mapping->a_ops = &jfs_aops;
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 mark_inode_dirty(ip);
151
Deepa Dinamani078cd822016-09-14 07:48:04 -0700152 dip->i_ctime = dip->i_mtime = current_time(dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 mark_inode_dirty(dip);
155
156 rc = txCommit(tid, 2, &iplist[0], 0);
157
158 out3:
159 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600160 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500161 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (rc) {
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500163 free_ea_wmap(ip);
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200164 clear_nlink(ip);
Al Viroa6cbedf2018-06-29 11:59:37 -0400165 discard_new_inode(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -0600166 } else {
Al Viro1e2e5472018-05-04 08:23:01 -0400167 d_instantiate_new(dentry, ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -0600168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 out2:
171 free_UCSname(&dname);
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 out1:
174
175 jfs_info("jfs_create: rc:%d", rc);
176 return rc;
177}
178
179
180/*
181 * NAME: jfs_mkdir(dip, dentry, mode)
182 *
183 * FUNCTION: create a child directory in the parent directory <dip>
184 * with name = <from dentry> and mode = <mode>
185 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500186 * PARAMETER: dip - parent directory vnode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 * dentry - dentry of child directory
188 * mode - create mode (rwxrwxrwx).
189 *
190 * RETURN: Errors from subroutines
191 *
192 * note:
Colin Ian Kinga83722f2018-10-26 19:09:09 +0100193 * EACCES: user needs search+write permission on the parent directory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 */
Al Viro18bb1db2011-07-26 01:41:39 -0400195static int jfs_mkdir(struct inode *dip, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 int rc = 0;
198 tid_t tid; /* transaction id */
199 struct inode *ip = NULL; /* child directory inode */
200 ino_t ino;
201 struct component_name dname; /* child directory name */
202 struct btstack btstack;
203 struct inode *iplist[2];
204 struct tblock *tblk;
205
Al Viroa4555892014-10-21 20:11:25 -0400206 jfs_info("jfs_mkdir: dip:0x%p name:%pd", dip, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Dave Kleikampacc84b02015-07-15 13:53:19 -0500208 rc = dquot_initialize(dip);
209 if (rc)
210 goto out1;
Christoph Hellwig907f4552010-03-03 09:05:06 -0500211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /*
213 * search parent directory for entry/freespace
214 * (dtSearch() returns parent directory page pinned)
215 */
216 if ((rc = get_UCSname(&dname, dentry)))
217 goto out1;
218
219 /*
220 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
221 * block there while holding dtree page, so we allocate the inode &
222 * begin the transaction before we search the directory.
223 */
224 ip = ialloc(dip, S_IFDIR | mode);
Akinobu Mita087387f2006-09-14 09:22:38 -0500225 if (IS_ERR(ip)) {
226 rc = PTR_ERR(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 goto out2;
228 }
229
230 tid = txBegin(dip->i_sb, 0);
231
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600232 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
233 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500235 rc = jfs_init_acl(tid, ip, dip);
236 if (rc)
237 goto out3;
238
Eric Paris2a7dba32011-02-01 11:05:39 -0500239 rc = jfs_init_security(tid, ip, dip, &dentry->d_name);
Dave Kleikamp1d15b10f2005-09-01 09:05:39 -0500240 if (rc) {
241 txAbort(tid, 0);
242 goto out3;
243 }
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
246 jfs_err("jfs_mkdir: dtSearch returned %d", rc);
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500247 txAbort(tid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 goto out3;
249 }
250
251 tblk = tid_to_tblock(tid);
252 tblk->xflag |= COMMIT_CREATE;
253 tblk->ino = ip->i_ino;
254 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
255
256 iplist[0] = dip;
257 iplist[1] = ip;
258
259 /*
260 * initialize the child directory in-line in inode
261 */
262 dtInitRoot(tid, ip, dip->i_ino);
263
264 /*
265 * create entry in parent directory for child directory
266 * (dtInsert() releases parent directory page)
267 */
268 ino = ip->i_ino;
269 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
270 if (rc == -EIO) {
271 jfs_err("jfs_mkdir: dtInsert returned -EIO");
272 txAbort(tid, 1); /* Marks Filesystem dirty */
273 } else
274 txAbort(tid, 0); /* Filesystem full */
275 goto out3;
276 }
277
Miklos Szeredibfe86842011-10-28 14:13:29 +0200278 set_nlink(ip, 2); /* for '.' */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 ip->i_op = &jfs_dir_inode_operations;
280 ip->i_fop = &jfs_dir_operations;
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 mark_inode_dirty(ip);
283
284 /* update parent directory inode */
Dave Hansend8c76e62006-09-30 23:29:04 -0700285 inc_nlink(dip); /* for '..' from child directory */
Deepa Dinamani078cd822016-09-14 07:48:04 -0700286 dip->i_ctime = dip->i_mtime = current_time(dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 mark_inode_dirty(dip);
288
289 rc = txCommit(tid, 2, &iplist[0], 0);
290
291 out3:
292 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600293 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500294 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (rc) {
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500296 free_ea_wmap(ip);
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200297 clear_nlink(ip);
Al Viroa6cbedf2018-06-29 11:59:37 -0400298 discard_new_inode(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -0600299 } else {
Al Viro1e2e5472018-05-04 08:23:01 -0400300 d_instantiate_new(dentry, ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -0600301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 out2:
304 free_UCSname(&dname);
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 out1:
308
309 jfs_info("jfs_mkdir: rc:%d", rc);
310 return rc;
311}
312
313/*
314 * NAME: jfs_rmdir(dip, dentry)
315 *
316 * FUNCTION: remove a link to child directory
317 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500318 * PARAMETER: dip - parent inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 * dentry - child directory dentry
320 *
321 * RETURN: -EINVAL - if name is . or ..
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500322 * -EINVAL - if . or .. exist but are invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 * errors from subroutines
324 *
325 * note:
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500326 * if other threads have the directory open when the last link
327 * is removed, the "." and ".." entries, if present, are removed before
328 * rmdir() returns and no new entries may be created in the directory,
329 * but the directory is not removed until the last reference to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 * the directory is released (cf.unlink() of regular file).
331 */
332static int jfs_rmdir(struct inode *dip, struct dentry *dentry)
333{
334 int rc;
335 tid_t tid; /* transaction id */
David Howells2b0143b2015-03-17 22:25:59 +0000336 struct inode *ip = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 ino_t ino;
338 struct component_name dname;
339 struct inode *iplist[2];
340 struct tblock *tblk;
341
Al Viroa4555892014-10-21 20:11:25 -0400342 jfs_info("jfs_rmdir: dip:0x%p name:%pd", dip, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 /* Init inode for quota operations. */
Dave Kleikampacc84b02015-07-15 13:53:19 -0500345 rc = dquot_initialize(dip);
346 if (rc)
347 goto out;
348 rc = dquot_initialize(ip);
349 if (rc)
350 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 /* directory must be empty to be removed */
353 if (!dtEmpty(ip)) {
354 rc = -ENOTEMPTY;
355 goto out;
356 }
357
358 if ((rc = get_UCSname(&dname, dentry))) {
359 goto out;
360 }
361
362 tid = txBegin(dip->i_sb, 0);
363
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600364 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
365 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 iplist[0] = dip;
368 iplist[1] = ip;
369
370 tblk = tid_to_tblock(tid);
371 tblk->xflag |= COMMIT_DELETE;
372 tblk->u.ip = ip;
373
374 /*
375 * delete the entry of target directory from parent directory
376 */
377 ino = ip->i_ino;
378 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
379 jfs_err("jfs_rmdir: dtDelete returned %d", rc);
380 if (rc == -EIO)
381 txAbort(tid, 1);
382 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600383 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500384 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 goto out2;
387 }
388
389 /* update parent directory's link count corresponding
390 * to ".." entry of the target directory deleted
391 */
Deepa Dinamani078cd822016-09-14 07:48:04 -0700392 dip->i_ctime = dip->i_mtime = current_time(dip);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700393 inode_dec_link_count(dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 /*
396 * OS/2 could have created EA and/or ACL
397 */
398 /* free EA from both persistent and working map */
399 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
400 /* free EA pages */
401 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
402 }
403 JFS_IP(ip)->ea.flag = 0;
404
405 /* free ACL from both persistent and working map */
406 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
407 /* free ACL pages */
408 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
409 }
410 JFS_IP(ip)->acl.flag = 0;
411
412 /* mark the target directory as deleted */
Dave Hansence71ec32006-09-30 23:29:06 -0700413 clear_nlink(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 mark_inode_dirty(ip);
415
416 rc = txCommit(tid, 2, &iplist[0], 0);
417
418 txEnd(tid);
419
Ingo Molnar1de87442006-01-24 15:22:50 -0600420 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500421 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 /*
424 * Truncating the directory index table is not guaranteed. It
425 * may need to be done iteratively
426 */
427 if (test_cflag(COMMIT_Stale, dip)) {
428 if (dip->i_size > 1)
429 jfs_truncate_nolock(dip, 0);
430
431 clear_cflag(COMMIT_Stale, dip);
432 }
433
434 out2:
435 free_UCSname(&dname);
436
437 out:
438 jfs_info("jfs_rmdir: rc:%d", rc);
439 return rc;
440}
441
442/*
443 * NAME: jfs_unlink(dip, dentry)
444 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500445 * FUNCTION: remove a link to object <vp> named by <name>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 * from parent directory <dvp>
447 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500448 * PARAMETER: dip - inode of parent directory
449 * dentry - dentry of object to be removed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 *
451 * RETURN: errors from subroutines
452 *
453 * note:
454 * temporary file: if one or more processes have the file open
455 * when the last link is removed, the link will be removed before
456 * unlink() returns, but the removal of the file contents will be
457 * postponed until all references to the files are closed.
458 *
459 * JFS does NOT support unlink() on directories.
460 *
461 */
462static int jfs_unlink(struct inode *dip, struct dentry *dentry)
463{
464 int rc;
465 tid_t tid; /* transaction id */
David Howells2b0143b2015-03-17 22:25:59 +0000466 struct inode *ip = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 ino_t ino;
468 struct component_name dname; /* object name */
469 struct inode *iplist[2];
470 struct tblock *tblk;
471 s64 new_size = 0;
472 int commit_flag;
473
Al Viroa4555892014-10-21 20:11:25 -0400474 jfs_info("jfs_unlink: dip:0x%p name:%pd", dip, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 /* Init inode for quota operations. */
Dave Kleikampacc84b02015-07-15 13:53:19 -0500477 rc = dquot_initialize(dip);
478 if (rc)
479 goto out;
480 rc = dquot_initialize(ip);
481 if (rc)
482 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 if ((rc = get_UCSname(&dname, dentry)))
485 goto out;
486
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600487 IWRITE_LOCK(ip, RDWRLOCK_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 tid = txBegin(dip->i_sb, 0);
490
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600491 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
492 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 iplist[0] = dip;
495 iplist[1] = ip;
496
497 /*
498 * delete the entry of target file from parent directory
499 */
500 ino = ip->i_ino;
501 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
502 jfs_err("jfs_unlink: dtDelete returned %d", rc);
503 if (rc == -EIO)
504 txAbort(tid, 1); /* Marks FS Dirty */
505 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600506 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500507 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 IWRITE_UNLOCK(ip);
509 goto out1;
510 }
511
512 ASSERT(ip->i_nlink);
513
Deepa Dinamani078cd822016-09-14 07:48:04 -0700514 ip->i_ctime = dip->i_ctime = dip->i_mtime = current_time(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 mark_inode_dirty(dip);
516
517 /* update target's inode */
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700518 inode_dec_link_count(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 /*
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500521 * commit zero link count object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 */
523 if (ip->i_nlink == 0) {
524 assert(!test_cflag(COMMIT_Nolink, ip));
525 /* free block resources */
526 if ((new_size = commitZeroLink(tid, ip)) < 0) {
527 txAbort(tid, 1); /* Marks FS Dirty */
528 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600529 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500530 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 IWRITE_UNLOCK(ip);
532 rc = new_size;
533 goto out1;
534 }
535 tblk = tid_to_tblock(tid);
536 tblk->xflag |= COMMIT_DELETE;
537 tblk->u.ip = ip;
538 }
539
540 /*
541 * Incomplete truncate of file data can
542 * result in timing problems unless we synchronously commit the
543 * transaction.
544 */
545 if (new_size)
546 commit_flag = COMMIT_SYNC;
547 else
548 commit_flag = 0;
549
550 /*
551 * If xtTruncate was incomplete, commit synchronously to avoid
552 * timing complications
553 */
554 rc = txCommit(tid, 2, &iplist[0], commit_flag);
555
556 txEnd(tid);
557
Ingo Molnar1de87442006-01-24 15:22:50 -0600558 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500559 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 while (new_size && (rc == 0)) {
562 tid = txBegin(dip->i_sb, 0);
Ingo Molnar1de87442006-01-24 15:22:50 -0600563 mutex_lock(&JFS_IP(ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 new_size = xtTruncate_pmap(tid, ip, new_size);
565 if (new_size < 0) {
566 txAbort(tid, 1); /* Marks FS Dirty */
567 rc = new_size;
568 } else
569 rc = txCommit(tid, 2, &iplist[0], COMMIT_SYNC);
570 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600571 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
573
574 if (ip->i_nlink == 0)
575 set_cflag(COMMIT_Nolink, ip);
576
577 IWRITE_UNLOCK(ip);
578
579 /*
580 * Truncating the directory index table is not guaranteed. It
581 * may need to be done iteratively
582 */
583 if (test_cflag(COMMIT_Stale, dip)) {
584 if (dip->i_size > 1)
585 jfs_truncate_nolock(dip, 0);
586
587 clear_cflag(COMMIT_Stale, dip);
588 }
589
590 out1:
591 free_UCSname(&dname);
592 out:
593 jfs_info("jfs_unlink: rc:%d", rc);
594 return rc;
595}
596
597/*
598 * NAME: commitZeroLink()
599 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500600 * FUNCTION: for non-directory, called by jfs_remove(),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 * truncate a regular file, directory or symbolic
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500602 * link to zero length. return 0 if type is not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 * one of these.
604 *
605 * if the file is currently associated with a VM segment
606 * only permanent disk and inode map resources are freed,
607 * and neither the inode nor indirect blocks are modified
608 * so that the resources can be later freed in the work
609 * map by ctrunc1.
610 * if there is no VM segment on entry, the resources are
611 * freed in both work and permanent map.
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500612 * (? for temporary file - memory object is cached even
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 * after no reference:
614 * reference count > 0 - )
615 *
616 * PARAMETERS: cd - pointer to commit data structure.
617 * current inode is the one to truncate.
618 *
619 * RETURN: Errors from subroutines
620 */
621static s64 commitZeroLink(tid_t tid, struct inode *ip)
622{
623 int filetype;
624 struct tblock *tblk;
625
626 jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid, ip);
627
628 filetype = ip->i_mode & S_IFMT;
629 switch (filetype) {
630 case S_IFREG:
631 break;
632 case S_IFLNK:
633 /* fast symbolic link */
634 if (ip->i_size < IDATASIZE) {
635 ip->i_size = 0;
636 return 0;
637 }
638 break;
639 default:
640 assert(filetype != S_IFDIR);
641 return 0;
642 }
643
644 set_cflag(COMMIT_Freewmap, ip);
645
646 /* mark transaction of block map update type */
647 tblk = tid_to_tblock(tid);
648 tblk->xflag |= COMMIT_PMAP;
649
650 /*
651 * free EA
652 */
653 if (JFS_IP(ip)->ea.flag & DXD_EXTENT)
654 /* acquire maplock on EA to be freed from block map */
655 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
656
657 /*
658 * free ACL
659 */
660 if (JFS_IP(ip)->acl.flag & DXD_EXTENT)
661 /* acquire maplock on EA to be freed from block map */
662 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
663
664 /*
665 * free xtree/data (truncate to zero length):
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500666 * free xtree/data pages from cache if COMMIT_PWMAP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 * free xtree/data blocks from persistent block map, and
668 * free xtree/data blocks from working block map if COMMIT_PWMAP;
669 */
670 if (ip->i_size)
671 return xtTruncate_pmap(tid, ip, 0);
672
673 return 0;
674}
675
676
677/*
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500678 * NAME: jfs_free_zero_link()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500680 * FUNCTION: for non-directory, called by iClose(),
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500681 * free resources of a file from cache and WORKING map
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 * for a file previously committed with zero link count
683 * while associated with a pager object,
684 *
685 * PARAMETER: ip - pointer to inode of file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 */
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500687void jfs_free_zero_link(struct inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 int type;
690
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500691 jfs_info("jfs_free_zero_link: ip = 0x%p", ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 /* return if not reg or symbolic link or if size is
694 * already ok.
695 */
696 type = ip->i_mode & S_IFMT;
697
698 switch (type) {
699 case S_IFREG:
700 break;
701 case S_IFLNK:
702 /* if its contained in inode nothing to do */
703 if (ip->i_size < IDATASIZE)
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500704 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 break;
706 default:
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500707 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709
710 /*
711 * free EA
712 */
713 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
714 s64 xaddr = addressDXD(&JFS_IP(ip)->ea);
715 int xlen = lengthDXD(&JFS_IP(ip)->ea);
716 struct maplock maplock; /* maplock for COMMIT_WMAP */
717 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
718
719 /* free EA pages from cache */
720 invalidate_dxd_metapages(ip, JFS_IP(ip)->ea);
721
722 /* free EA extent from working block map */
723 maplock.index = 1;
724 pxdlock = (struct pxd_lock *) & maplock;
725 pxdlock->flag = mlckFREEPXD;
726 PXDaddress(&pxdlock->pxd, xaddr);
727 PXDlength(&pxdlock->pxd, xlen);
728 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
729 }
730
731 /*
732 * free ACL
733 */
734 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
735 s64 xaddr = addressDXD(&JFS_IP(ip)->acl);
736 int xlen = lengthDXD(&JFS_IP(ip)->acl);
737 struct maplock maplock; /* maplock for COMMIT_WMAP */
738 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
739
740 invalidate_dxd_metapages(ip, JFS_IP(ip)->acl);
741
742 /* free ACL extent from working block map */
743 maplock.index = 1;
744 pxdlock = (struct pxd_lock *) & maplock;
745 pxdlock->flag = mlckFREEPXD;
746 PXDaddress(&pxdlock->pxd, xaddr);
747 PXDlength(&pxdlock->pxd, xlen);
748 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
749 }
750
751 /*
752 * free xtree/data (truncate to zero length):
753 * free xtree/data pages from cache, and
754 * free xtree/data blocks from working block map;
755 */
756 if (ip->i_size)
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500757 xtTruncate(0, ip, 0, COMMIT_WMAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
760/*
761 * NAME: jfs_link(vp, dvp, name, crp)
762 *
763 * FUNCTION: create a link to <vp> by the name = <name>
764 * in the parent directory <dvp>
765 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500766 * PARAMETER: vp - target object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 * dvp - parent directory of new link
768 * name - name of new link to target object
769 * crp - credential
770 *
771 * RETURN: Errors from subroutines
772 *
773 * note:
774 * JFS does NOT support link() on directories (to prevent circular
775 * path in the directory hierarchy);
776 * EPERM: the target object is a directory, and either the caller
777 * does not have appropriate privileges or the implementation prohibits
778 * using link() on directories [XPG4.2].
779 *
780 * JFS does NOT support links between file systems:
781 * EXDEV: target object and new link are on different file systems and
782 * implementation does not support links between file systems [XPG4.2].
783 */
784static int jfs_link(struct dentry *old_dentry,
785 struct inode *dir, struct dentry *dentry)
786{
787 int rc;
788 tid_t tid;
David Howells2b0143b2015-03-17 22:25:59 +0000789 struct inode *ip = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 ino_t ino;
791 struct component_name dname;
792 struct btstack btstack;
793 struct inode *iplist[2];
794
Al Viroa4555892014-10-21 20:11:25 -0400795 jfs_info("jfs_link: %pd %pd", old_dentry, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Dave Kleikampacc84b02015-07-15 13:53:19 -0500797 rc = dquot_initialize(dir);
798 if (rc)
799 goto out;
Christoph Hellwig907f4552010-03-03 09:05:06 -0500800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 tid = txBegin(ip->i_sb, 0);
802
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600803 mutex_lock_nested(&JFS_IP(dir)->commit_mutex, COMMIT_MUTEX_PARENT);
804 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 /*
807 * scan parent directory for entry/freespace
808 */
809 if ((rc = get_UCSname(&dname, dentry)))
Dave Kleikampacc84b02015-07-15 13:53:19 -0500810 goto out_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
813 goto free_dname;
814
815 /*
816 * create entry for new link in parent directory
817 */
818 ino = ip->i_ino;
819 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
820 goto free_dname;
821
822 /* update object inode */
Dave Hansend8c76e62006-09-30 23:29:04 -0700823 inc_nlink(ip); /* for new link */
Deepa Dinamani078cd822016-09-14 07:48:04 -0700824 ip->i_ctime = current_time(ip);
825 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 mark_inode_dirty(dir);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400827 ihold(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 iplist[0] = ip;
830 iplist[1] = dir;
831 rc = txCommit(tid, 2, &iplist[0], 0);
832
833 if (rc) {
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200834 drop_nlink(ip); /* never instantiated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 iput(ip);
836 } else
837 d_instantiate(dentry, ip);
838
839 free_dname:
840 free_UCSname(&dname);
841
Dave Kleikampacc84b02015-07-15 13:53:19 -0500842 out_tx:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 txEnd(tid);
844
Ingo Molnar1de87442006-01-24 15:22:50 -0600845 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500846 mutex_unlock(&JFS_IP(dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Dave Kleikampacc84b02015-07-15 13:53:19 -0500848 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 jfs_info("jfs_link: rc:%d", rc);
850 return rc;
851}
852
853/*
854 * NAME: jfs_symlink(dip, dentry, name)
855 *
856 * FUNCTION: creates a symbolic link to <symlink> by name <name>
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500857 * in directory <dip>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500859 * PARAMETER: dip - parent directory vnode
860 * dentry - dentry of symbolic link
861 * name - the path name of the existing object
862 * that will be the source of the link
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 *
864 * RETURN: errors from subroutines
865 *
866 * note:
867 * ENAMETOOLONG: pathname resolution of a symbolic link produced
868 * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
869*/
870
871static int jfs_symlink(struct inode *dip, struct dentry *dentry,
872 const char *name)
873{
874 int rc;
875 tid_t tid;
876 ino_t ino = 0;
877 struct component_name dname;
878 int ssize; /* source pathname size */
879 struct btstack btstack;
David Howells2b0143b2015-03-17 22:25:59 +0000880 struct inode *ip = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 s64 xlen = 0;
882 int bmask = 0, xsize;
Dave Kleikamp3c2c2262011-06-20 13:00:27 -0500883 s64 xaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 struct metapage *mp;
885 struct super_block *sb;
886 struct tblock *tblk;
887
888 struct inode *iplist[2];
889
890 jfs_info("jfs_symlink: dip:0x%p name:%s", dip, name);
891
Dave Kleikampacc84b02015-07-15 13:53:19 -0500892 rc = dquot_initialize(dip);
893 if (rc)
894 goto out1;
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;
Al Viro21fc61c2015-11-17 01:07:57 -0500969 inode_nohighmem(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 ip->i_mapping->a_ops = &jfs_aops;
971
972 /*
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500973 * even though the data of symlink object (source
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 * path name) is treated as non-journaled user data,
975 * it is read/written thru buffer cache for performance.
976 */
977 sb = ip->i_sb;
978 bmask = JFS_SBI(sb)->bsize - 1;
979 xsize = (ssize + bmask) & ~bmask;
980 xaddr = 0;
981 xlen = xsize >> JFS_SBI(sb)->l2bsize;
982 if ((rc = xtInsert(tid, ip, 0, 0, xlen, &xaddr, 0))) {
983 txAbort(tid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 goto out3;
985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 ip->i_size = ssize - 1;
987 while (ssize) {
988 /* This is kind of silly since PATH_MAX == 4K */
989 int copy_size = min(ssize, PSIZE);
990
991 mp = get_metapage(ip, xaddr, PSIZE, 1);
992
993 if (mp == NULL) {
994 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
995 rc = -EIO;
996 txAbort(tid, 0);
997 goto out3;
998 }
999 memcpy(mp->data, name, copy_size);
1000 flush_metapage(mp);
1001 ssize -= copy_size;
1002 name += copy_size;
1003 xaddr += JFS_SBI(sb)->nbperpage;
1004 }
1005 }
1006
1007 /*
1008 * create entry for symbolic link in parent directory
1009 */
1010 rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE);
1011 if (rc == 0) {
1012 ino = ip->i_ino;
1013 rc = dtInsert(tid, dip, &dname, &ino, &btstack);
1014 }
1015 if (rc) {
1016 if (xlen)
1017 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
1018 txAbort(tid, 0);
1019 /* discard new inode */
1020 goto out3;
1021 }
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 mark_inode_dirty(ip);
1024
Deepa Dinamani078cd822016-09-14 07:48:04 -07001025 dip->i_ctime = dip->i_mtime = current_time(dip);
Dave Kleikamp988a6492005-10-31 16:53:04 -06001026 mark_inode_dirty(dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 /*
1028 * commit update of parent directory and link object
1029 */
1030
1031 iplist[0] = dip;
1032 iplist[1] = ip;
1033 rc = txCommit(tid, 2, &iplist[0], 0);
1034
1035 out3:
1036 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -06001037 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -05001038 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 if (rc) {
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001040 free_ea_wmap(ip);
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02001041 clear_nlink(ip);
Al Viroa6cbedf2018-06-29 11:59:37 -04001042 discard_new_inode(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001043 } else {
Al Viro1e2e5472018-05-04 08:23:01 -04001044 d_instantiate_new(dentry, ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 out2:
1048 free_UCSname(&dname);
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 out1:
1051 jfs_info("jfs_symlink: rc:%d", rc);
1052 return rc;
1053}
1054
1055
1056/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001057 * NAME: jfs_rename
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001059 * FUNCTION: rename a file or directory
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 */
1061static int jfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredif03b8ad2016-09-27 11:03:57 +02001062 struct inode *new_dir, struct dentry *new_dentry,
1063 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
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
Miklos Szeredif03b8ad2016-09-27 11:03:57 +02001082 if (flags & ~RENAME_NOREPLACE)
1083 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Al Viroa4555892014-10-21 20:11:25 -04001085 jfs_info("jfs_rename: %pd %pd", old_dentry, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Dave Kleikampacc84b02015-07-15 13:53:19 -05001087 rc = dquot_initialize(old_dir);
1088 if (rc)
1089 goto out1;
1090 rc = dquot_initialize(new_dir);
1091 if (rc)
1092 goto out1;
Christoph Hellwig907f4552010-03-03 09:05:06 -05001093
David Howells2b0143b2015-03-17 22:25:59 +00001094 old_ip = d_inode(old_dentry);
1095 new_ip = d_inode(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 if ((rc = get_UCSname(&old_dname, old_dentry)))
1098 goto out1;
1099
1100 if ((rc = get_UCSname(&new_dname, new_dentry)))
1101 goto out2;
1102
1103 /*
1104 * Make sure source inode number is what we think it is
1105 */
1106 rc = dtSearch(old_dir, &old_dname, &ino, &btstack, JFS_LOOKUP);
1107 if (rc || (ino != old_ip->i_ino)) {
1108 rc = -ENOENT;
1109 goto out3;
1110 }
1111
1112 /*
1113 * Make sure dest inode number (if any) is what we think it is
1114 */
1115 rc = dtSearch(new_dir, &new_dname, &ino, &btstack, JFS_LOOKUP);
Joe Perches09aaa742007-11-13 22:16:08 -06001116 if (!rc) {
Dave Kleikampda8a41d2007-11-13 22:25:41 -06001117 if ((!new_ip) || (ino != new_ip->i_ino)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 rc = -ESTALE;
1119 goto out3;
1120 }
1121 } else if (rc != -ENOENT)
1122 goto out3;
1123 else if (new_ip) {
1124 /* no entry exists, but one was expected */
1125 rc = -ESTALE;
1126 goto out3;
1127 }
1128
1129 if (S_ISDIR(old_ip->i_mode)) {
1130 if (new_ip) {
1131 if (!dtEmpty(new_ip)) {
1132 rc = -ENOTEMPTY;
1133 goto out3;
1134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
1136 } else if (new_ip) {
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001137 IWRITE_LOCK(new_ip, RDWRLOCK_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 /* Init inode for quota operations. */
Dave Kleikampacc84b02015-07-15 13:53:19 -05001139 rc = dquot_initialize(new_ip);
1140 if (rc)
1141 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143
1144 /*
1145 * The real work starts here
1146 */
1147 tid = txBegin(new_dir->i_sb, 0);
1148
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001149 /*
1150 * How do we know the locking is safe from deadlocks?
1151 * The vfs does the hard part for us. Any time we are taking nested
1152 * commit_mutexes, the vfs already has i_mutex held on the parent.
1153 * Here, the vfs has already taken i_mutex on both old_dir and new_dir.
1154 */
1155 mutex_lock_nested(&JFS_IP(new_dir)->commit_mutex, COMMIT_MUTEX_PARENT);
1156 mutex_lock_nested(&JFS_IP(old_ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (old_dir != new_dir)
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001158 mutex_lock_nested(&JFS_IP(old_dir)->commit_mutex,
1159 COMMIT_MUTEX_SECOND_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 if (new_ip) {
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001162 mutex_lock_nested(&JFS_IP(new_ip)->commit_mutex,
1163 COMMIT_MUTEX_VICTIM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 /*
1165 * Change existing directory entry to new inode number
1166 */
1167 ino = new_ip->i_ino;
1168 rc = dtModify(tid, new_dir, &new_dname, &ino,
1169 old_ip->i_ino, JFS_RENAME);
1170 if (rc)
Dave Kleikamp26456952015-07-15 12:52:47 -05001171 goto out_tx;
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001172 drop_nlink(new_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if (S_ISDIR(new_ip->i_mode)) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001174 drop_nlink(new_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 if (new_ip->i_nlink) {
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -05001176 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (old_dir != new_dir)
Ingo Molnar1de87442006-01-24 15:22:50 -06001178 mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -05001179 mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
1180 mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (!S_ISDIR(old_ip->i_mode) && new_ip)
1182 IWRITE_UNLOCK(new_ip);
1183 jfs_error(new_ip->i_sb,
Joe Percheseb8630d2013-06-04 16:39:15 -07001184 "new_ip->i_nlink != 0\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return -EIO;
1186 }
1187 tblk = tid_to_tblock(tid);
1188 tblk->xflag |= COMMIT_DELETE;
1189 tblk->u.ip = new_ip;
1190 } else if (new_ip->i_nlink == 0) {
1191 assert(!test_cflag(COMMIT_Nolink, new_ip));
1192 /* free block resources */
1193 if ((new_size = commitZeroLink(tid, new_ip)) < 0) {
1194 txAbort(tid, 1); /* Marks FS Dirty */
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001195 rc = new_size;
Dave Kleikamp26456952015-07-15 12:52:47 -05001196 goto out_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 }
1198 tblk = tid_to_tblock(tid);
1199 tblk->xflag |= COMMIT_DELETE;
1200 tblk->u.ip = new_ip;
1201 } else {
Deepa Dinamani078cd822016-09-14 07:48:04 -07001202 new_ip->i_ctime = current_time(new_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 mark_inode_dirty(new_ip);
1204 }
1205 } else {
1206 /*
1207 * Add new directory entry
1208 */
1209 rc = dtSearch(new_dir, &new_dname, &ino, &btstack,
1210 JFS_CREATE);
1211 if (rc) {
Joe Perches6ed71e92016-03-30 05:23:18 -07001212 jfs_err("jfs_rename didn't expect dtSearch to fail w/rc = %d",
1213 rc);
Dave Kleikamp26456952015-07-15 12:52:47 -05001214 goto out_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
1216
1217 ino = old_ip->i_ino;
1218 rc = dtInsert(tid, new_dir, &new_dname, &ino, &btstack);
1219 if (rc) {
1220 if (rc == -EIO)
1221 jfs_err("jfs_rename: dtInsert returned -EIO");
Dave Kleikamp26456952015-07-15 12:52:47 -05001222 goto out_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224 if (S_ISDIR(old_ip->i_mode))
Dave Hansend8c76e62006-09-30 23:29:04 -07001225 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
1227 /*
1228 * Remove old directory entry
1229 */
1230
1231 ino = old_ip->i_ino;
1232 rc = dtDelete(tid, old_dir, &old_dname, &ino, JFS_REMOVE);
1233 if (rc) {
1234 jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
1235 rc);
1236 txAbort(tid, 1); /* Marks Filesystem dirty */
Dave Kleikamp26456952015-07-15 12:52:47 -05001237 goto out_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 }
1239 if (S_ISDIR(old_ip->i_mode)) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001240 drop_nlink(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 if (old_dir != new_dir) {
1242 /*
1243 * Change inode number of parent for moved directory
1244 */
1245
1246 JFS_IP(old_ip)->i_dtroot.header.idotdot =
1247 cpu_to_le32(new_dir->i_ino);
1248
1249 /* Linelock header of dtree */
1250 tlck = txLock(tid, old_ip,
1251 (struct metapage *) &JFS_IP(old_ip)->bxflag,
1252 tlckDTREE | tlckBTROOT | tlckRELINK);
1253 dtlck = (struct dt_lock *) & tlck->lock;
1254 ASSERT(dtlck->index == 0);
1255 lv = & dtlck->lv[0];
1256 lv->offset = 0;
1257 lv->length = 1;
1258 dtlck->index++;
1259 }
1260 }
1261
1262 /*
1263 * Update ctime on changed/moved inodes & mark dirty
1264 */
Deepa Dinamani078cd822016-09-14 07:48:04 -07001265 old_ip->i_ctime = current_time(old_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 mark_inode_dirty(old_ip);
1267
Deepa Dinamanic2050a42016-09-14 07:48:06 -07001268 new_dir->i_ctime = new_dir->i_mtime = current_time(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 mark_inode_dirty(new_dir);
1270
1271 /* Build list of inodes modified by this transaction */
1272 ipcount = 0;
1273 iplist[ipcount++] = old_ip;
1274 if (new_ip)
1275 iplist[ipcount++] = new_ip;
1276 iplist[ipcount++] = old_dir;
1277
1278 if (old_dir != new_dir) {
1279 iplist[ipcount++] = new_dir;
Deepa Dinamani078cd822016-09-14 07:48:04 -07001280 old_dir->i_ctime = old_dir->i_mtime = current_time(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 mark_inode_dirty(old_dir);
1282 }
1283
1284 /*
1285 * Incomplete truncate of file data can
1286 * result in timing problems unless we synchronously commit the
1287 * transaction.
1288 */
1289 if (new_size)
1290 commit_flag = COMMIT_SYNC;
1291 else
1292 commit_flag = 0;
1293
1294 rc = txCommit(tid, ipcount, iplist, commit_flag);
1295
Dave Kleikamp26456952015-07-15 12:52:47 -05001296 out_tx:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 txEnd(tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 if (new_ip)
Ingo Molnar1de87442006-01-24 15:22:50 -06001299 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -05001300 if (old_dir != new_dir)
1301 mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
1302 mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
1303 mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 while (new_size && (rc == 0)) {
1306 tid = txBegin(new_ip->i_sb, 0);
Ingo Molnar1de87442006-01-24 15:22:50 -06001307 mutex_lock(&JFS_IP(new_ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 new_size = xtTruncate_pmap(tid, new_ip, new_size);
1309 if (new_size < 0) {
1310 txAbort(tid, 1);
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001311 rc = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 } else
1313 rc = txCommit(tid, 1, &new_ip, COMMIT_SYNC);
1314 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -06001315 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
1317 if (new_ip && (new_ip->i_nlink == 0))
1318 set_cflag(COMMIT_Nolink, new_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 /*
1320 * Truncating the directory index table is not guaranteed. It
1321 * may need to be done iteratively
1322 */
1323 if (test_cflag(COMMIT_Stale, old_dir)) {
1324 if (old_dir->i_size > 1)
1325 jfs_truncate_nolock(old_dir, 0);
1326
1327 clear_cflag(COMMIT_Stale, old_dir);
1328 }
Dave Kleikampacc84b02015-07-15 13:53:19 -05001329 out_unlock:
Dave Kleikamp26456952015-07-15 12:52:47 -05001330 if (new_ip && !S_ISDIR(new_ip->i_mode))
1331 IWRITE_UNLOCK(new_ip);
1332 out3:
1333 free_UCSname(&new_dname);
1334 out2:
1335 free_UCSname(&old_dname);
1336 out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 jfs_info("jfs_rename: returning %d", rc);
1338 return rc;
1339}
1340
1341
1342/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001343 * NAME: jfs_mknod
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001345 * FUNCTION: Create a special file (device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 */
1347static int jfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04001348 umode_t mode, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
1350 struct jfs_inode_info *jfs_ip;
1351 struct btstack btstack;
1352 struct component_name dname;
1353 ino_t ino;
1354 struct inode *ip;
1355 struct inode *iplist[2];
1356 int rc;
1357 tid_t tid;
1358 struct tblock *tblk;
1359
Al Viroa4555892014-10-21 20:11:25 -04001360 jfs_info("jfs_mknod: %pd", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Dave Kleikampacc84b02015-07-15 13:53:19 -05001362 rc = dquot_initialize(dir);
1363 if (rc)
1364 goto out;
Christoph Hellwig907f4552010-03-03 09:05:06 -05001365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if ((rc = get_UCSname(&dname, dentry)))
1367 goto out;
1368
1369 ip = ialloc(dir, mode);
Akinobu Mita087387f2006-09-14 09:22:38 -05001370 if (IS_ERR(ip)) {
1371 rc = PTR_ERR(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 goto out1;
1373 }
1374 jfs_ip = JFS_IP(ip);
1375
1376 tid = txBegin(dir->i_sb, 0);
1377
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001378 mutex_lock_nested(&JFS_IP(dir)->commit_mutex, COMMIT_MUTEX_PARENT);
1379 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001381 rc = jfs_init_acl(tid, ip, dir);
1382 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 goto out3;
1384
Eric Paris2a7dba32011-02-01 11:05:39 -05001385 rc = jfs_init_security(tid, ip, dir, &dentry->d_name);
Dave Kleikamp1d15b10f2005-09-01 09:05:39 -05001386 if (rc) {
1387 txAbort(tid, 0);
1388 goto out3;
1389 }
1390
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001391 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE))) {
1392 txAbort(tid, 0);
1393 goto out3;
1394 }
1395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 tblk = tid_to_tblock(tid);
1397 tblk->xflag |= COMMIT_CREATE;
1398 tblk->ino = ip->i_ino;
1399 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
1400
1401 ino = ip->i_ino;
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001402 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack))) {
1403 txAbort(tid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 goto out3;
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 ip->i_op = &jfs_file_inode_operations;
1408 jfs_ip->dev = new_encode_dev(rdev);
1409 init_special_inode(ip, ip->i_mode, rdev);
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 mark_inode_dirty(ip);
1412
Deepa Dinamani078cd822016-09-14 07:48:04 -07001413 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 mark_inode_dirty(dir);
1416
1417 iplist[0] = dir;
1418 iplist[1] = ip;
1419 rc = txCommit(tid, 2, iplist, 0);
1420
1421 out3:
1422 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -06001423 mutex_unlock(&JFS_IP(ip)->commit_mutex);
1424 mutex_unlock(&JFS_IP(dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (rc) {
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001426 free_ea_wmap(ip);
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02001427 clear_nlink(ip);
Al Viroa6cbedf2018-06-29 11:59:37 -04001428 discard_new_inode(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001429 } else {
Al Viro1e2e5472018-05-04 08:23:01 -04001430 d_instantiate_new(dentry, ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 out1:
1434 free_UCSname(&dname);
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 out:
1437 jfs_info("jfs_mknod: returning %d", rc);
1438 return rc;
1439}
1440
Al Viro00cd8dd2012-06-10 17:13:09 -04001441static struct dentry *jfs_lookup(struct inode *dip, struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
1443 struct btstack btstack;
1444 ino_t inum;
1445 struct inode *ip;
1446 struct component_name key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 int rc;
1448
Al Viroa4555892014-10-21 20:11:25 -04001449 jfs_info("jfs_lookup: name = %pd", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Al Viro79ac5a42011-07-17 10:17:46 -04001451 if ((rc = get_UCSname(&key, dentry)))
1452 return ERR_PTR(rc);
1453 rc = dtSearch(dip, &key, &inum, &btstack, JFS_LOOKUP);
1454 free_UCSname(&key);
1455 if (rc == -ENOENT) {
1456 ip = NULL;
1457 } else if (rc) {
1458 jfs_err("jfs_lookup: dtSearch returned %d", rc);
1459 ip = ERR_PTR(rc);
1460 } else {
1461 ip = jfs_iget(dip->i_sb, inum);
1462 if (IS_ERR(ip))
1463 jfs_err("jfs_lookup: iget failed on inum %d", (uint)inum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 }
1465
Al Viro94b77bd2010-12-18 10:59:31 -05001466 return d_splice_alias(ip, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467}
1468
Christoph Hellwigd425de72007-10-21 16:42:09 -07001469static struct inode *jfs_nfs_get_inode(struct super_block *sb,
1470 u64 ino, u32 generation)
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001471{
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001472 struct inode *inode;
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001473
1474 if (ino == 0)
1475 return ERR_PTR(-ESTALE);
David Howellseab1df72008-02-07 00:15:43 -08001476 inode = jfs_iget(sb, ino);
1477 if (IS_ERR(inode))
1478 return ERR_CAST(inode);
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001479
David Howellseab1df72008-02-07 00:15:43 -08001480 if (generation && inode->i_generation != generation) {
Christoph Hellwigd425de72007-10-21 16:42:09 -07001481 iput(inode);
1482 return ERR_PTR(-ESTALE);
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001483 }
1484
Christoph Hellwigd425de72007-10-21 16:42:09 -07001485 return inode;
1486}
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001487
Christoph Hellwigd425de72007-10-21 16:42:09 -07001488struct dentry *jfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
1489 int fh_len, int fh_type)
1490{
1491 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1492 jfs_nfs_get_inode);
1493}
1494
1495struct dentry *jfs_fh_to_parent(struct super_block *sb, struct fid *fid,
1496 int fh_len, int fh_type)
1497{
1498 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1499 jfs_nfs_get_inode);
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001500}
1501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502struct dentry *jfs_get_parent(struct dentry *dentry)
1503{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 unsigned long parent_ino;
1505
1506 parent_ino =
David Howells2b0143b2015-03-17 22:25:59 +00001507 le32_to_cpu(JFS_IP(d_inode(dentry))->i_dtroot.header.idotdot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Al Virofc640052016-04-10 01:33:30 -04001509 return d_obtain_alias(jfs_iget(dentry->d_sb, parent_ino));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
1511
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001512const struct inode_operations jfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 .create = jfs_create,
1514 .lookup = jfs_lookup,
1515 .link = jfs_link,
1516 .unlink = jfs_unlink,
1517 .symlink = jfs_symlink,
1518 .mkdir = jfs_mkdir,
1519 .rmdir = jfs_rmdir,
1520 .mknod = jfs_mknod,
1521 .rename = jfs_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 .listxattr = jfs_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 .setattr = jfs_setattr,
Christoph Hellwig759bfee2010-03-03 09:05:02 -05001524#ifdef CONFIG_JFS_POSIX_ACL
Christoph Hellwig4e34e712011-07-23 17:37:31 +02001525 .get_acl = jfs_get_acl,
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -08001526 .set_acl = jfs_set_acl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527#endif
1528};
1529
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001530const struct file_operations jfs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 .read = generic_read_dir,
Al Viro070a0eb2013-05-17 17:00:34 -04001532 .iterate = jfs_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 .fsync = jfs_fsync,
Andi Kleenbaab81f2008-01-27 16:58:51 -06001534 .unlocked_ioctl = jfs_ioctl,
Andi Kleenef1fc2f2008-01-27 17:02:02 -06001535#ifdef CONFIG_COMPAT
1536 .compat_ioctl = jfs_compat_ioctl,
1537#endif
Christoph Hellwig3222a3e2008-09-03 21:53:01 +02001538 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539};
1540
Linus Torvaldsda53be12013-05-21 15:22:44 -07001541static int jfs_ci_hash(const struct dentry *dir, struct qstr *this)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
1543 unsigned long hash;
1544 int i;
1545
Linus Torvalds8387ff22016-06-10 07:51:30 -07001546 hash = init_name_hash(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 for (i=0; i < this->len; i++)
1548 hash = partial_name_hash(tolower(this->name[i]), hash);
1549 this->hash = end_name_hash(hash);
1550
1551 return 0;
1552}
1553
Al Viro6fa67e72016-07-31 16:37:25 -04001554static int jfs_ci_compare(const struct dentry *dentry,
Nick Piggin621e1552011-01-07 17:49:27 +11001555 unsigned int len, const char *str, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
1557 int i, result = 1;
1558
Nick Piggin621e1552011-01-07 17:49:27 +11001559 if (len != name->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 goto out;
Nick Piggin621e1552011-01-07 17:49:27 +11001561 for (i=0; i < len; i++) {
1562 if (tolower(str[i]) != tolower(name->name[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 goto out;
1564 }
1565 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566out:
1567 return result;
1568}
1569
Al Viro0b728e12012-06-10 16:03:43 -04001570static int jfs_ci_revalidate(struct dentry *dentry, unsigned int flags)
Nick Piggin2bc334d2011-01-07 17:49:25 +11001571{
1572 /*
1573 * This is not negative dentry. Always valid.
1574 *
1575 * Note, rename() to existing directory entry will have ->d_inode,
1576 * and will use existing name which isn't specified name by user.
1577 *
1578 * We may be able to drop this positive dentry here. But dropping
1579 * positive dentry isn't good idea. So it's unsupported like
1580 * rename("filename", "FILENAME") for now.
1581 */
David Howells2b0143b2015-03-17 22:25:59 +00001582 if (d_really_is_positive(dentry))
Nick Piggin2bc334d2011-01-07 17:49:25 +11001583 return 1;
1584
1585 /*
1586 * This may be nfsd (or something), anyway, we can't see the
1587 * intent of this. So, since this can be for creation, drop it.
1588 */
Al Viro0b728e12012-06-10 16:03:43 -04001589 if (!flags)
Nick Piggin2bc334d2011-01-07 17:49:25 +11001590 return 0;
1591
1592 /*
1593 * Drop the negative dentry, in order to make sure to use the
1594 * case sensitive name which is specified by user if this is
1595 * for creation.
1596 */
Al Viro0b728e12012-06-10 16:03:43 -04001597 if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
Al Viro407938e2011-06-25 21:37:18 -04001598 return 0;
Nick Piggin2bc334d2011-01-07 17:49:25 +11001599 return 1;
1600}
1601
Al Viroad28b4e2009-02-20 06:00:49 +00001602const struct dentry_operations jfs_ci_dentry_operations =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
1604 .d_hash = jfs_ci_hash,
1605 .d_compare = jfs_ci_compare,
Nick Piggin2bc334d2011-01-07 17:49:25 +11001606 .d_revalidate = jfs_ci_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607};