blob: 14528c0ffe635a30cb1a3efb403e4f2a429d5034 [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
Dave Kleikampacc84b02015-07-15 13:53:19 -050089 rc = dquot_initialize(dip);
90 if (rc)
91 goto out1;
Christoph Hellwig907f4552010-03-03 09:05:06 -050092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 /*
94 * search parent directory for entry/freespace
95 * (dtSearch() returns parent directory page pinned)
96 */
97 if ((rc = get_UCSname(&dname, dentry)))
98 goto out1;
99
100 /*
101 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
102 * block there while holding dtree page, so we allocate the inode &
103 * begin the transaction before we search the directory.
104 */
105 ip = ialloc(dip, mode);
Akinobu Mita087387f2006-09-14 09:22:38 -0500106 if (IS_ERR(ip)) {
107 rc = PTR_ERR(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 goto out2;
109 }
110
111 tid = txBegin(dip->i_sb, 0);
112
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600113 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
114 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500116 rc = jfs_init_acl(tid, ip, dip);
117 if (rc)
118 goto out3;
119
Eric Paris2a7dba32011-02-01 11:05:39 -0500120 rc = jfs_init_security(tid, ip, dip, &dentry->d_name);
Dave Kleikamp1d15b10f2005-09-01 09:05:39 -0500121 if (rc) {
122 txAbort(tid, 0);
123 goto out3;
124 }
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
127 jfs_err("jfs_create: dtSearch returned %d", rc);
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500128 txAbort(tid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 goto out3;
130 }
131
132 tblk = tid_to_tblock(tid);
133 tblk->xflag |= COMMIT_CREATE;
134 tblk->ino = ip->i_ino;
135 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
136
137 iplist[0] = dip;
138 iplist[1] = ip;
139
140 /*
141 * initialize the child XAD tree root in-line in inode
142 */
143 xtInitRoot(tid, ip);
144
145 /*
146 * create entry in parent directory for child directory
147 * (dtInsert() releases parent directory page)
148 */
149 ino = ip->i_ino;
150 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
151 if (rc == -EIO) {
152 jfs_err("jfs_create: dtInsert returned -EIO");
153 txAbort(tid, 1); /* Marks Filesystem dirty */
154 } else
155 txAbort(tid, 0); /* Filesystem full */
156 goto out3;
157 }
158
159 ip->i_op = &jfs_file_inode_operations;
160 ip->i_fop = &jfs_file_operations;
161 ip->i_mapping->a_ops = &jfs_aops;
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 mark_inode_dirty(ip);
164
Deepa Dinamani078cd822016-09-14 07:48:04 -0700165 dip->i_ctime = dip->i_mtime = current_time(dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 mark_inode_dirty(dip);
168
169 rc = txCommit(tid, 2, &iplist[0], 0);
170
171 out3:
172 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600173 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500174 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (rc) {
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500176 free_ea_wmap(ip);
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200177 clear_nlink(ip);
Al Viroa6cbedf2018-06-29 11:59:37 -0400178 discard_new_inode(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -0600179 } else {
Al Viro1e2e5472018-05-04 08:23:01 -0400180 d_instantiate_new(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
Dave Kleikampacc84b02015-07-15 13:53:19 -0500221 rc = dquot_initialize(dip);
222 if (rc)
223 goto out1;
Christoph Hellwig907f4552010-03-03 09:05:06 -0500224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 /*
226 * search parent directory for entry/freespace
227 * (dtSearch() returns parent directory page pinned)
228 */
229 if ((rc = get_UCSname(&dname, dentry)))
230 goto out1;
231
232 /*
233 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
234 * block there while holding dtree page, so we allocate the inode &
235 * begin the transaction before we search the directory.
236 */
237 ip = ialloc(dip, S_IFDIR | mode);
Akinobu Mita087387f2006-09-14 09:22:38 -0500238 if (IS_ERR(ip)) {
239 rc = PTR_ERR(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 goto out2;
241 }
242
243 tid = txBegin(dip->i_sb, 0);
244
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600245 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
246 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500248 rc = jfs_init_acl(tid, ip, dip);
249 if (rc)
250 goto out3;
251
Eric Paris2a7dba32011-02-01 11:05:39 -0500252 rc = jfs_init_security(tid, ip, dip, &dentry->d_name);
Dave Kleikamp1d15b10f2005-09-01 09:05:39 -0500253 if (rc) {
254 txAbort(tid, 0);
255 goto out3;
256 }
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
259 jfs_err("jfs_mkdir: dtSearch returned %d", rc);
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500260 txAbort(tid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 goto out3;
262 }
263
264 tblk = tid_to_tblock(tid);
265 tblk->xflag |= COMMIT_CREATE;
266 tblk->ino = ip->i_ino;
267 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
268
269 iplist[0] = dip;
270 iplist[1] = ip;
271
272 /*
273 * initialize the child directory in-line in inode
274 */
275 dtInitRoot(tid, ip, dip->i_ino);
276
277 /*
278 * create entry in parent directory for child directory
279 * (dtInsert() releases parent directory page)
280 */
281 ino = ip->i_ino;
282 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
283 if (rc == -EIO) {
284 jfs_err("jfs_mkdir: dtInsert returned -EIO");
285 txAbort(tid, 1); /* Marks Filesystem dirty */
286 } else
287 txAbort(tid, 0); /* Filesystem full */
288 goto out3;
289 }
290
Miklos Szeredibfe86842011-10-28 14:13:29 +0200291 set_nlink(ip, 2); /* for '.' */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 ip->i_op = &jfs_dir_inode_operations;
293 ip->i_fop = &jfs_dir_operations;
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 mark_inode_dirty(ip);
296
297 /* update parent directory inode */
Dave Hansend8c76e62006-09-30 23:29:04 -0700298 inc_nlink(dip); /* for '..' from child directory */
Deepa Dinamani078cd822016-09-14 07:48:04 -0700299 dip->i_ctime = dip->i_mtime = current_time(dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 mark_inode_dirty(dip);
301
302 rc = txCommit(tid, 2, &iplist[0], 0);
303
304 out3:
305 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600306 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500307 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (rc) {
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500309 free_ea_wmap(ip);
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200310 clear_nlink(ip);
Al Viroa6cbedf2018-06-29 11:59:37 -0400311 discard_new_inode(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -0600312 } else {
Al Viro1e2e5472018-05-04 08:23:01 -0400313 d_instantiate_new(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. */
Dave Kleikampacc84b02015-07-15 13:53:19 -0500358 rc = dquot_initialize(dip);
359 if (rc)
360 goto out;
361 rc = dquot_initialize(ip);
362 if (rc)
363 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 /* directory must be empty to be removed */
366 if (!dtEmpty(ip)) {
367 rc = -ENOTEMPTY;
368 goto out;
369 }
370
371 if ((rc = get_UCSname(&dname, dentry))) {
372 goto out;
373 }
374
375 tid = txBegin(dip->i_sb, 0);
376
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600377 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
378 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 iplist[0] = dip;
381 iplist[1] = ip;
382
383 tblk = tid_to_tblock(tid);
384 tblk->xflag |= COMMIT_DELETE;
385 tblk->u.ip = ip;
386
387 /*
388 * delete the entry of target directory from parent directory
389 */
390 ino = ip->i_ino;
391 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
392 jfs_err("jfs_rmdir: dtDelete returned %d", rc);
393 if (rc == -EIO)
394 txAbort(tid, 1);
395 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600396 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500397 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 goto out2;
400 }
401
402 /* update parent directory's link count corresponding
403 * to ".." entry of the target directory deleted
404 */
Deepa Dinamani078cd822016-09-14 07:48:04 -0700405 dip->i_ctime = dip->i_mtime = current_time(dip);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700406 inode_dec_link_count(dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 /*
409 * OS/2 could have created EA and/or ACL
410 */
411 /* free EA from both persistent and working map */
412 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
413 /* free EA pages */
414 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
415 }
416 JFS_IP(ip)->ea.flag = 0;
417
418 /* free ACL from both persistent and working map */
419 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
420 /* free ACL pages */
421 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
422 }
423 JFS_IP(ip)->acl.flag = 0;
424
425 /* mark the target directory as deleted */
Dave Hansence71ec32006-09-30 23:29:06 -0700426 clear_nlink(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 mark_inode_dirty(ip);
428
429 rc = txCommit(tid, 2, &iplist[0], 0);
430
431 txEnd(tid);
432
Ingo Molnar1de87442006-01-24 15:22:50 -0600433 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500434 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 /*
437 * Truncating the directory index table is not guaranteed. It
438 * may need to be done iteratively
439 */
440 if (test_cflag(COMMIT_Stale, dip)) {
441 if (dip->i_size > 1)
442 jfs_truncate_nolock(dip, 0);
443
444 clear_cflag(COMMIT_Stale, dip);
445 }
446
447 out2:
448 free_UCSname(&dname);
449
450 out:
451 jfs_info("jfs_rmdir: rc:%d", rc);
452 return rc;
453}
454
455/*
456 * NAME: jfs_unlink(dip, dentry)
457 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500458 * FUNCTION: remove a link to object <vp> named by <name>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 * from parent directory <dvp>
460 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500461 * PARAMETER: dip - inode of parent directory
462 * dentry - dentry of object to be removed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 *
464 * RETURN: errors from subroutines
465 *
466 * note:
467 * temporary file: if one or more processes have the file open
468 * when the last link is removed, the link will be removed before
469 * unlink() returns, but the removal of the file contents will be
470 * postponed until all references to the files are closed.
471 *
472 * JFS does NOT support unlink() on directories.
473 *
474 */
475static int jfs_unlink(struct inode *dip, struct dentry *dentry)
476{
477 int rc;
478 tid_t tid; /* transaction id */
David Howells2b0143b2015-03-17 22:25:59 +0000479 struct inode *ip = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 ino_t ino;
481 struct component_name dname; /* object name */
482 struct inode *iplist[2];
483 struct tblock *tblk;
484 s64 new_size = 0;
485 int commit_flag;
486
Al Viroa4555892014-10-21 20:11:25 -0400487 jfs_info("jfs_unlink: dip:0x%p name:%pd", dip, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 /* Init inode for quota operations. */
Dave Kleikampacc84b02015-07-15 13:53:19 -0500490 rc = dquot_initialize(dip);
491 if (rc)
492 goto out;
493 rc = dquot_initialize(ip);
494 if (rc)
495 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 if ((rc = get_UCSname(&dname, dentry)))
498 goto out;
499
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600500 IWRITE_LOCK(ip, RDWRLOCK_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 tid = txBegin(dip->i_sb, 0);
503
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600504 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
505 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 iplist[0] = dip;
508 iplist[1] = ip;
509
510 /*
511 * delete the entry of target file from parent directory
512 */
513 ino = ip->i_ino;
514 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
515 jfs_err("jfs_unlink: dtDelete returned %d", rc);
516 if (rc == -EIO)
517 txAbort(tid, 1); /* Marks FS Dirty */
518 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600519 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500520 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 IWRITE_UNLOCK(ip);
522 goto out1;
523 }
524
525 ASSERT(ip->i_nlink);
526
Deepa Dinamani078cd822016-09-14 07:48:04 -0700527 ip->i_ctime = dip->i_ctime = dip->i_mtime = current_time(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 mark_inode_dirty(dip);
529
530 /* update target's inode */
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700531 inode_dec_link_count(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 /*
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500534 * commit zero link count object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 */
536 if (ip->i_nlink == 0) {
537 assert(!test_cflag(COMMIT_Nolink, ip));
538 /* free block resources */
539 if ((new_size = commitZeroLink(tid, ip)) < 0) {
540 txAbort(tid, 1); /* Marks FS Dirty */
541 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600542 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500543 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 IWRITE_UNLOCK(ip);
545 rc = new_size;
546 goto out1;
547 }
548 tblk = tid_to_tblock(tid);
549 tblk->xflag |= COMMIT_DELETE;
550 tblk->u.ip = ip;
551 }
552
553 /*
554 * Incomplete truncate of file data can
555 * result in timing problems unless we synchronously commit the
556 * transaction.
557 */
558 if (new_size)
559 commit_flag = COMMIT_SYNC;
560 else
561 commit_flag = 0;
562
563 /*
564 * If xtTruncate was incomplete, commit synchronously to avoid
565 * timing complications
566 */
567 rc = txCommit(tid, 2, &iplist[0], commit_flag);
568
569 txEnd(tid);
570
Ingo Molnar1de87442006-01-24 15:22:50 -0600571 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500572 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 while (new_size && (rc == 0)) {
575 tid = txBegin(dip->i_sb, 0);
Ingo Molnar1de87442006-01-24 15:22:50 -0600576 mutex_lock(&JFS_IP(ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 new_size = xtTruncate_pmap(tid, ip, new_size);
578 if (new_size < 0) {
579 txAbort(tid, 1); /* Marks FS Dirty */
580 rc = new_size;
581 } else
582 rc = txCommit(tid, 2, &iplist[0], COMMIT_SYNC);
583 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600584 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
586
587 if (ip->i_nlink == 0)
588 set_cflag(COMMIT_Nolink, ip);
589
590 IWRITE_UNLOCK(ip);
591
592 /*
593 * Truncating the directory index table is not guaranteed. It
594 * may need to be done iteratively
595 */
596 if (test_cflag(COMMIT_Stale, dip)) {
597 if (dip->i_size > 1)
598 jfs_truncate_nolock(dip, 0);
599
600 clear_cflag(COMMIT_Stale, dip);
601 }
602
603 out1:
604 free_UCSname(&dname);
605 out:
606 jfs_info("jfs_unlink: rc:%d", rc);
607 return rc;
608}
609
610/*
611 * NAME: commitZeroLink()
612 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500613 * FUNCTION: for non-directory, called by jfs_remove(),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 * truncate a regular file, directory or symbolic
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500615 * link to zero length. return 0 if type is not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 * one of these.
617 *
618 * if the file is currently associated with a VM segment
619 * only permanent disk and inode map resources are freed,
620 * and neither the inode nor indirect blocks are modified
621 * so that the resources can be later freed in the work
622 * map by ctrunc1.
623 * if there is no VM segment on entry, the resources are
624 * freed in both work and permanent map.
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500625 * (? for temporary file - memory object is cached even
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 * after no reference:
627 * reference count > 0 - )
628 *
629 * PARAMETERS: cd - pointer to commit data structure.
630 * current inode is the one to truncate.
631 *
632 * RETURN: Errors from subroutines
633 */
634static s64 commitZeroLink(tid_t tid, struct inode *ip)
635{
636 int filetype;
637 struct tblock *tblk;
638
639 jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid, ip);
640
641 filetype = ip->i_mode & S_IFMT;
642 switch (filetype) {
643 case S_IFREG:
644 break;
645 case S_IFLNK:
646 /* fast symbolic link */
647 if (ip->i_size < IDATASIZE) {
648 ip->i_size = 0;
649 return 0;
650 }
651 break;
652 default:
653 assert(filetype != S_IFDIR);
654 return 0;
655 }
656
657 set_cflag(COMMIT_Freewmap, ip);
658
659 /* mark transaction of block map update type */
660 tblk = tid_to_tblock(tid);
661 tblk->xflag |= COMMIT_PMAP;
662
663 /*
664 * free EA
665 */
666 if (JFS_IP(ip)->ea.flag & DXD_EXTENT)
667 /* acquire maplock on EA to be freed from block map */
668 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
669
670 /*
671 * free ACL
672 */
673 if (JFS_IP(ip)->acl.flag & DXD_EXTENT)
674 /* acquire maplock on EA to be freed from block map */
675 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
676
677 /*
678 * free xtree/data (truncate to zero length):
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500679 * free xtree/data pages from cache if COMMIT_PWMAP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 * free xtree/data blocks from persistent block map, and
681 * free xtree/data blocks from working block map if COMMIT_PWMAP;
682 */
683 if (ip->i_size)
684 return xtTruncate_pmap(tid, ip, 0);
685
686 return 0;
687}
688
689
690/*
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500691 * NAME: jfs_free_zero_link()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500693 * FUNCTION: for non-directory, called by iClose(),
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500694 * free resources of a file from cache and WORKING map
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 * for a file previously committed with zero link count
696 * while associated with a pager object,
697 *
698 * PARAMETER: ip - pointer to inode of file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 */
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500700void jfs_free_zero_link(struct inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 int type;
703
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500704 jfs_info("jfs_free_zero_link: ip = 0x%p", ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 /* return if not reg or symbolic link or if size is
707 * already ok.
708 */
709 type = ip->i_mode & S_IFMT;
710
711 switch (type) {
712 case S_IFREG:
713 break;
714 case S_IFLNK:
715 /* if its contained in inode nothing to do */
716 if (ip->i_size < IDATASIZE)
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500717 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 break;
719 default:
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500720 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
722
723 /*
724 * free EA
725 */
726 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
727 s64 xaddr = addressDXD(&JFS_IP(ip)->ea);
728 int xlen = lengthDXD(&JFS_IP(ip)->ea);
729 struct maplock maplock; /* maplock for COMMIT_WMAP */
730 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
731
732 /* free EA pages from cache */
733 invalidate_dxd_metapages(ip, JFS_IP(ip)->ea);
734
735 /* free EA extent from working block map */
736 maplock.index = 1;
737 pxdlock = (struct pxd_lock *) & maplock;
738 pxdlock->flag = mlckFREEPXD;
739 PXDaddress(&pxdlock->pxd, xaddr);
740 PXDlength(&pxdlock->pxd, xlen);
741 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
742 }
743
744 /*
745 * free ACL
746 */
747 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
748 s64 xaddr = addressDXD(&JFS_IP(ip)->acl);
749 int xlen = lengthDXD(&JFS_IP(ip)->acl);
750 struct maplock maplock; /* maplock for COMMIT_WMAP */
751 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
752
753 invalidate_dxd_metapages(ip, JFS_IP(ip)->acl);
754
755 /* free ACL extent from working block map */
756 maplock.index = 1;
757 pxdlock = (struct pxd_lock *) & maplock;
758 pxdlock->flag = mlckFREEPXD;
759 PXDaddress(&pxdlock->pxd, xaddr);
760 PXDlength(&pxdlock->pxd, xlen);
761 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
762 }
763
764 /*
765 * free xtree/data (truncate to zero length):
766 * free xtree/data pages from cache, and
767 * free xtree/data blocks from working block map;
768 */
769 if (ip->i_size)
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500770 xtTruncate(0, ip, 0, COMMIT_WMAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
773/*
774 * NAME: jfs_link(vp, dvp, name, crp)
775 *
776 * FUNCTION: create a link to <vp> by the name = <name>
777 * in the parent directory <dvp>
778 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500779 * PARAMETER: vp - target object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 * dvp - parent directory of new link
781 * name - name of new link to target object
782 * crp - credential
783 *
784 * RETURN: Errors from subroutines
785 *
786 * note:
787 * JFS does NOT support link() on directories (to prevent circular
788 * path in the directory hierarchy);
789 * EPERM: the target object is a directory, and either the caller
790 * does not have appropriate privileges or the implementation prohibits
791 * using link() on directories [XPG4.2].
792 *
793 * JFS does NOT support links between file systems:
794 * EXDEV: target object and new link are on different file systems and
795 * implementation does not support links between file systems [XPG4.2].
796 */
797static int jfs_link(struct dentry *old_dentry,
798 struct inode *dir, struct dentry *dentry)
799{
800 int rc;
801 tid_t tid;
David Howells2b0143b2015-03-17 22:25:59 +0000802 struct inode *ip = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 ino_t ino;
804 struct component_name dname;
805 struct btstack btstack;
806 struct inode *iplist[2];
807
Al Viroa4555892014-10-21 20:11:25 -0400808 jfs_info("jfs_link: %pd %pd", old_dentry, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Dave Kleikampacc84b02015-07-15 13:53:19 -0500810 rc = dquot_initialize(dir);
811 if (rc)
812 goto out;
Christoph Hellwig907f4552010-03-03 09:05:06 -0500813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 tid = txBegin(ip->i_sb, 0);
815
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600816 mutex_lock_nested(&JFS_IP(dir)->commit_mutex, COMMIT_MUTEX_PARENT);
817 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 /*
820 * scan parent directory for entry/freespace
821 */
822 if ((rc = get_UCSname(&dname, dentry)))
Dave Kleikampacc84b02015-07-15 13:53:19 -0500823 goto out_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
826 goto free_dname;
827
828 /*
829 * create entry for new link in parent directory
830 */
831 ino = ip->i_ino;
832 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
833 goto free_dname;
834
835 /* update object inode */
Dave Hansend8c76e62006-09-30 23:29:04 -0700836 inc_nlink(ip); /* for new link */
Deepa Dinamani078cd822016-09-14 07:48:04 -0700837 ip->i_ctime = current_time(ip);
838 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 mark_inode_dirty(dir);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400840 ihold(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 iplist[0] = ip;
843 iplist[1] = dir;
844 rc = txCommit(tid, 2, &iplist[0], 0);
845
846 if (rc) {
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200847 drop_nlink(ip); /* never instantiated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 iput(ip);
849 } else
850 d_instantiate(dentry, ip);
851
852 free_dname:
853 free_UCSname(&dname);
854
Dave Kleikampacc84b02015-07-15 13:53:19 -0500855 out_tx:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 txEnd(tid);
857
Ingo Molnar1de87442006-01-24 15:22:50 -0600858 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500859 mutex_unlock(&JFS_IP(dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Dave Kleikampacc84b02015-07-15 13:53:19 -0500861 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 jfs_info("jfs_link: rc:%d", rc);
863 return rc;
864}
865
866/*
867 * NAME: jfs_symlink(dip, dentry, name)
868 *
869 * FUNCTION: creates a symbolic link to <symlink> by name <name>
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500870 * in directory <dip>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500872 * PARAMETER: dip - parent directory vnode
873 * dentry - dentry of symbolic link
874 * name - the path name of the existing object
875 * that will be the source of the link
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 *
877 * RETURN: errors from subroutines
878 *
879 * note:
880 * ENAMETOOLONG: pathname resolution of a symbolic link produced
881 * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
882*/
883
884static int jfs_symlink(struct inode *dip, struct dentry *dentry,
885 const char *name)
886{
887 int rc;
888 tid_t tid;
889 ino_t ino = 0;
890 struct component_name dname;
891 int ssize; /* source pathname size */
892 struct btstack btstack;
David Howells2b0143b2015-03-17 22:25:59 +0000893 struct inode *ip = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 s64 xlen = 0;
895 int bmask = 0, xsize;
Dave Kleikamp3c2c2262011-06-20 13:00:27 -0500896 s64 xaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 struct metapage *mp;
898 struct super_block *sb;
899 struct tblock *tblk;
900
901 struct inode *iplist[2];
902
903 jfs_info("jfs_symlink: dip:0x%p name:%s", dip, name);
904
Dave Kleikampacc84b02015-07-15 13:53:19 -0500905 rc = dquot_initialize(dip);
906 if (rc)
907 goto out1;
Christoph Hellwig907f4552010-03-03 09:05:06 -0500908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 ssize = strlen(name) + 1;
910
911 /*
912 * search parent directory for entry/freespace
913 * (dtSearch() returns parent directory page pinned)
914 */
915
916 if ((rc = get_UCSname(&dname, dentry)))
917 goto out1;
918
919 /*
920 * allocate on-disk/in-memory inode for symbolic link:
921 * (iAlloc() returns new, locked inode)
922 */
923 ip = ialloc(dip, S_IFLNK | 0777);
Akinobu Mita087387f2006-09-14 09:22:38 -0500924 if (IS_ERR(ip)) {
925 rc = PTR_ERR(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 goto out2;
927 }
928
929 tid = txBegin(dip->i_sb, 0);
930
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600931 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
932 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Eric Paris2a7dba32011-02-01 11:05:39 -0500934 rc = jfs_init_security(tid, ip, dip, &dentry->d_name);
Dave Kleikamp1d15b10f2005-09-01 09:05:39 -0500935 if (rc)
936 goto out3;
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 tblk = tid_to_tblock(tid);
939 tblk->xflag |= COMMIT_CREATE;
940 tblk->ino = ip->i_ino;
941 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
942
943 /* fix symlink access permission
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500944 * (dir_create() ANDs in the u.u_cmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 * but symlinks really need to be 777 access)
946 */
947 ip->i_mode |= 0777;
948
949 /*
950 * write symbolic link target path name
951 */
952 xtInitRoot(tid, ip);
953
954 /*
955 * write source path name inline in on-disk inode (fast symbolic link)
956 */
957
958 if (ssize <= IDATASIZE) {
Dmitry Monakhovc7f2e1f2010-04-16 08:05:50 -0500959 ip->i_op = &jfs_fast_symlink_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Al Viroad476fe2015-05-02 10:41:20 -0400961 ip->i_link = JFS_IP(ip)->i_inline;
962 memcpy(ip->i_link, name, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 ip->i_size = ssize - 1;
964
965 /*
966 * if symlink is > 128 bytes, we don't have the space to
967 * store inline extended attributes
968 */
969 if (ssize > sizeof (JFS_IP(ip)->i_inline))
970 JFS_IP(ip)->mode2 &= ~INLINEEA;
971
972 jfs_info("jfs_symlink: fast symlink added ssize:%d name:%s ",
973 ssize, name);
974 }
975 /*
976 * write source path name in a single extent
977 */
978 else {
979 jfs_info("jfs_symlink: allocate extent ip:0x%p", ip);
980
Dmitry Monakhovc7f2e1f2010-04-16 08:05:50 -0500981 ip->i_op = &jfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -0500982 inode_nohighmem(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 ip->i_mapping->a_ops = &jfs_aops;
984
985 /*
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500986 * even though the data of symlink object (source
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 * path name) is treated as non-journaled user data,
988 * it is read/written thru buffer cache for performance.
989 */
990 sb = ip->i_sb;
991 bmask = JFS_SBI(sb)->bsize - 1;
992 xsize = (ssize + bmask) & ~bmask;
993 xaddr = 0;
994 xlen = xsize >> JFS_SBI(sb)->l2bsize;
995 if ((rc = xtInsert(tid, ip, 0, 0, xlen, &xaddr, 0))) {
996 txAbort(tid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 goto out3;
998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 ip->i_size = ssize - 1;
1000 while (ssize) {
1001 /* This is kind of silly since PATH_MAX == 4K */
1002 int copy_size = min(ssize, PSIZE);
1003
1004 mp = get_metapage(ip, xaddr, PSIZE, 1);
1005
1006 if (mp == NULL) {
1007 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
1008 rc = -EIO;
1009 txAbort(tid, 0);
1010 goto out3;
1011 }
1012 memcpy(mp->data, name, copy_size);
1013 flush_metapage(mp);
1014 ssize -= copy_size;
1015 name += copy_size;
1016 xaddr += JFS_SBI(sb)->nbperpage;
1017 }
1018 }
1019
1020 /*
1021 * create entry for symbolic link in parent directory
1022 */
1023 rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE);
1024 if (rc == 0) {
1025 ino = ip->i_ino;
1026 rc = dtInsert(tid, dip, &dname, &ino, &btstack);
1027 }
1028 if (rc) {
1029 if (xlen)
1030 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
1031 txAbort(tid, 0);
1032 /* discard new inode */
1033 goto out3;
1034 }
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 mark_inode_dirty(ip);
1037
Deepa Dinamani078cd822016-09-14 07:48:04 -07001038 dip->i_ctime = dip->i_mtime = current_time(dip);
Dave Kleikamp988a6492005-10-31 16:53:04 -06001039 mark_inode_dirty(dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 /*
1041 * commit update of parent directory and link object
1042 */
1043
1044 iplist[0] = dip;
1045 iplist[1] = ip;
1046 rc = txCommit(tid, 2, &iplist[0], 0);
1047
1048 out3:
1049 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -06001050 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -05001051 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (rc) {
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001053 free_ea_wmap(ip);
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02001054 clear_nlink(ip);
Al Viroa6cbedf2018-06-29 11:59:37 -04001055 discard_new_inode(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001056 } else {
Al Viro1e2e5472018-05-04 08:23:01 -04001057 d_instantiate_new(dentry, ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 out2:
1061 free_UCSname(&dname);
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 out1:
1064 jfs_info("jfs_symlink: rc:%d", rc);
1065 return rc;
1066}
1067
1068
1069/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001070 * NAME: jfs_rename
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001072 * FUNCTION: rename a file or directory
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 */
1074static int jfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredif03b8ad2016-09-27 11:03:57 +02001075 struct inode *new_dir, struct dentry *new_dentry,
1076 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
1078 struct btstack btstack;
1079 ino_t ino;
1080 struct component_name new_dname;
1081 struct inode *new_ip;
1082 struct component_name old_dname;
1083 struct inode *old_ip;
1084 int rc;
1085 tid_t tid;
1086 struct tlock *tlck;
1087 struct dt_lock *dtlck;
1088 struct lv *lv;
1089 int ipcount;
1090 struct inode *iplist[4];
1091 struct tblock *tblk;
1092 s64 new_size = 0;
1093 int commit_flag;
1094
Miklos Szeredif03b8ad2016-09-27 11:03:57 +02001095 if (flags & ~RENAME_NOREPLACE)
1096 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Al Viroa4555892014-10-21 20:11:25 -04001098 jfs_info("jfs_rename: %pd %pd", old_dentry, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Dave Kleikampacc84b02015-07-15 13:53:19 -05001100 rc = dquot_initialize(old_dir);
1101 if (rc)
1102 goto out1;
1103 rc = dquot_initialize(new_dir);
1104 if (rc)
1105 goto out1;
Christoph Hellwig907f4552010-03-03 09:05:06 -05001106
David Howells2b0143b2015-03-17 22:25:59 +00001107 old_ip = d_inode(old_dentry);
1108 new_ip = d_inode(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 if ((rc = get_UCSname(&old_dname, old_dentry)))
1111 goto out1;
1112
1113 if ((rc = get_UCSname(&new_dname, new_dentry)))
1114 goto out2;
1115
1116 /*
1117 * Make sure source inode number is what we think it is
1118 */
1119 rc = dtSearch(old_dir, &old_dname, &ino, &btstack, JFS_LOOKUP);
1120 if (rc || (ino != old_ip->i_ino)) {
1121 rc = -ENOENT;
1122 goto out3;
1123 }
1124
1125 /*
1126 * Make sure dest inode number (if any) is what we think it is
1127 */
1128 rc = dtSearch(new_dir, &new_dname, &ino, &btstack, JFS_LOOKUP);
Joe Perches09aaa742007-11-13 22:16:08 -06001129 if (!rc) {
Dave Kleikampda8a41d2007-11-13 22:25:41 -06001130 if ((!new_ip) || (ino != new_ip->i_ino)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 rc = -ESTALE;
1132 goto out3;
1133 }
1134 } else if (rc != -ENOENT)
1135 goto out3;
1136 else if (new_ip) {
1137 /* no entry exists, but one was expected */
1138 rc = -ESTALE;
1139 goto out3;
1140 }
1141
1142 if (S_ISDIR(old_ip->i_mode)) {
1143 if (new_ip) {
1144 if (!dtEmpty(new_ip)) {
1145 rc = -ENOTEMPTY;
1146 goto out3;
1147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
1149 } else if (new_ip) {
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001150 IWRITE_LOCK(new_ip, RDWRLOCK_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 /* Init inode for quota operations. */
Dave Kleikampacc84b02015-07-15 13:53:19 -05001152 rc = dquot_initialize(new_ip);
1153 if (rc)
1154 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
1156
1157 /*
1158 * The real work starts here
1159 */
1160 tid = txBegin(new_dir->i_sb, 0);
1161
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001162 /*
1163 * How do we know the locking is safe from deadlocks?
1164 * The vfs does the hard part for us. Any time we are taking nested
1165 * commit_mutexes, the vfs already has i_mutex held on the parent.
1166 * Here, the vfs has already taken i_mutex on both old_dir and new_dir.
1167 */
1168 mutex_lock_nested(&JFS_IP(new_dir)->commit_mutex, COMMIT_MUTEX_PARENT);
1169 mutex_lock_nested(&JFS_IP(old_ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if (old_dir != new_dir)
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001171 mutex_lock_nested(&JFS_IP(old_dir)->commit_mutex,
1172 COMMIT_MUTEX_SECOND_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 if (new_ip) {
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001175 mutex_lock_nested(&JFS_IP(new_ip)->commit_mutex,
1176 COMMIT_MUTEX_VICTIM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 /*
1178 * Change existing directory entry to new inode number
1179 */
1180 ino = new_ip->i_ino;
1181 rc = dtModify(tid, new_dir, &new_dname, &ino,
1182 old_ip->i_ino, JFS_RENAME);
1183 if (rc)
Dave Kleikamp26456952015-07-15 12:52:47 -05001184 goto out_tx;
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001185 drop_nlink(new_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if (S_ISDIR(new_ip->i_mode)) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001187 drop_nlink(new_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 if (new_ip->i_nlink) {
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -05001189 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 if (old_dir != new_dir)
Ingo Molnar1de87442006-01-24 15:22:50 -06001191 mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -05001192 mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
1193 mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if (!S_ISDIR(old_ip->i_mode) && new_ip)
1195 IWRITE_UNLOCK(new_ip);
1196 jfs_error(new_ip->i_sb,
Joe Percheseb8630d2013-06-04 16:39:15 -07001197 "new_ip->i_nlink != 0\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 return -EIO;
1199 }
1200 tblk = tid_to_tblock(tid);
1201 tblk->xflag |= COMMIT_DELETE;
1202 tblk->u.ip = new_ip;
1203 } else if (new_ip->i_nlink == 0) {
1204 assert(!test_cflag(COMMIT_Nolink, new_ip));
1205 /* free block resources */
1206 if ((new_size = commitZeroLink(tid, new_ip)) < 0) {
1207 txAbort(tid, 1); /* Marks FS Dirty */
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001208 rc = new_size;
Dave Kleikamp26456952015-07-15 12:52:47 -05001209 goto out_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 }
1211 tblk = tid_to_tblock(tid);
1212 tblk->xflag |= COMMIT_DELETE;
1213 tblk->u.ip = new_ip;
1214 } else {
Deepa Dinamani078cd822016-09-14 07:48:04 -07001215 new_ip->i_ctime = current_time(new_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 mark_inode_dirty(new_ip);
1217 }
1218 } else {
1219 /*
1220 * Add new directory entry
1221 */
1222 rc = dtSearch(new_dir, &new_dname, &ino, &btstack,
1223 JFS_CREATE);
1224 if (rc) {
Joe Perches6ed71e92016-03-30 05:23:18 -07001225 jfs_err("jfs_rename didn't expect dtSearch to fail w/rc = %d",
1226 rc);
Dave Kleikamp26456952015-07-15 12:52:47 -05001227 goto out_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
1229
1230 ino = old_ip->i_ino;
1231 rc = dtInsert(tid, new_dir, &new_dname, &ino, &btstack);
1232 if (rc) {
1233 if (rc == -EIO)
1234 jfs_err("jfs_rename: dtInsert returned -EIO");
Dave Kleikamp26456952015-07-15 12:52:47 -05001235 goto out_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
1237 if (S_ISDIR(old_ip->i_mode))
Dave Hansend8c76e62006-09-30 23:29:04 -07001238 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240 /*
1241 * Remove old directory entry
1242 */
1243
1244 ino = old_ip->i_ino;
1245 rc = dtDelete(tid, old_dir, &old_dname, &ino, JFS_REMOVE);
1246 if (rc) {
1247 jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
1248 rc);
1249 txAbort(tid, 1); /* Marks Filesystem dirty */
Dave Kleikamp26456952015-07-15 12:52:47 -05001250 goto out_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 }
1252 if (S_ISDIR(old_ip->i_mode)) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001253 drop_nlink(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (old_dir != new_dir) {
1255 /*
1256 * Change inode number of parent for moved directory
1257 */
1258
1259 JFS_IP(old_ip)->i_dtroot.header.idotdot =
1260 cpu_to_le32(new_dir->i_ino);
1261
1262 /* Linelock header of dtree */
1263 tlck = txLock(tid, old_ip,
1264 (struct metapage *) &JFS_IP(old_ip)->bxflag,
1265 tlckDTREE | tlckBTROOT | tlckRELINK);
1266 dtlck = (struct dt_lock *) & tlck->lock;
1267 ASSERT(dtlck->index == 0);
1268 lv = & dtlck->lv[0];
1269 lv->offset = 0;
1270 lv->length = 1;
1271 dtlck->index++;
1272 }
1273 }
1274
1275 /*
1276 * Update ctime on changed/moved inodes & mark dirty
1277 */
Deepa Dinamani078cd822016-09-14 07:48:04 -07001278 old_ip->i_ctime = current_time(old_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 mark_inode_dirty(old_ip);
1280
Deepa Dinamanic2050a42016-09-14 07:48:06 -07001281 new_dir->i_ctime = new_dir->i_mtime = current_time(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 mark_inode_dirty(new_dir);
1283
1284 /* Build list of inodes modified by this transaction */
1285 ipcount = 0;
1286 iplist[ipcount++] = old_ip;
1287 if (new_ip)
1288 iplist[ipcount++] = new_ip;
1289 iplist[ipcount++] = old_dir;
1290
1291 if (old_dir != new_dir) {
1292 iplist[ipcount++] = new_dir;
Deepa Dinamani078cd822016-09-14 07:48:04 -07001293 old_dir->i_ctime = old_dir->i_mtime = current_time(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 mark_inode_dirty(old_dir);
1295 }
1296
1297 /*
1298 * Incomplete truncate of file data can
1299 * result in timing problems unless we synchronously commit the
1300 * transaction.
1301 */
1302 if (new_size)
1303 commit_flag = COMMIT_SYNC;
1304 else
1305 commit_flag = 0;
1306
1307 rc = txCommit(tid, ipcount, iplist, commit_flag);
1308
Dave Kleikamp26456952015-07-15 12:52:47 -05001309 out_tx:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 txEnd(tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (new_ip)
Ingo Molnar1de87442006-01-24 15:22:50 -06001312 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -05001313 if (old_dir != new_dir)
1314 mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
1315 mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
1316 mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 while (new_size && (rc == 0)) {
1319 tid = txBegin(new_ip->i_sb, 0);
Ingo Molnar1de87442006-01-24 15:22:50 -06001320 mutex_lock(&JFS_IP(new_ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 new_size = xtTruncate_pmap(tid, new_ip, new_size);
1322 if (new_size < 0) {
1323 txAbort(tid, 1);
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001324 rc = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 } else
1326 rc = txCommit(tid, 1, &new_ip, COMMIT_SYNC);
1327 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -06001328 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 }
1330 if (new_ip && (new_ip->i_nlink == 0))
1331 set_cflag(COMMIT_Nolink, new_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 /*
1333 * Truncating the directory index table is not guaranteed. It
1334 * may need to be done iteratively
1335 */
1336 if (test_cflag(COMMIT_Stale, old_dir)) {
1337 if (old_dir->i_size > 1)
1338 jfs_truncate_nolock(old_dir, 0);
1339
1340 clear_cflag(COMMIT_Stale, old_dir);
1341 }
Dave Kleikampacc84b02015-07-15 13:53:19 -05001342 out_unlock:
Dave Kleikamp26456952015-07-15 12:52:47 -05001343 if (new_ip && !S_ISDIR(new_ip->i_mode))
1344 IWRITE_UNLOCK(new_ip);
1345 out3:
1346 free_UCSname(&new_dname);
1347 out2:
1348 free_UCSname(&old_dname);
1349 out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 jfs_info("jfs_rename: returning %d", rc);
1351 return rc;
1352}
1353
1354
1355/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001356 * NAME: jfs_mknod
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001358 * FUNCTION: Create a special file (device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 */
1360static int jfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04001361 umode_t mode, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
1363 struct jfs_inode_info *jfs_ip;
1364 struct btstack btstack;
1365 struct component_name dname;
1366 ino_t ino;
1367 struct inode *ip;
1368 struct inode *iplist[2];
1369 int rc;
1370 tid_t tid;
1371 struct tblock *tblk;
1372
Al Viroa4555892014-10-21 20:11:25 -04001373 jfs_info("jfs_mknod: %pd", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Dave Kleikampacc84b02015-07-15 13:53:19 -05001375 rc = dquot_initialize(dir);
1376 if (rc)
1377 goto out;
Christoph Hellwig907f4552010-03-03 09:05:06 -05001378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 if ((rc = get_UCSname(&dname, dentry)))
1380 goto out;
1381
1382 ip = ialloc(dir, mode);
Akinobu Mita087387f2006-09-14 09:22:38 -05001383 if (IS_ERR(ip)) {
1384 rc = PTR_ERR(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 goto out1;
1386 }
1387 jfs_ip = JFS_IP(ip);
1388
1389 tid = txBegin(dir->i_sb, 0);
1390
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001391 mutex_lock_nested(&JFS_IP(dir)->commit_mutex, COMMIT_MUTEX_PARENT);
1392 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001394 rc = jfs_init_acl(tid, ip, dir);
1395 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 goto out3;
1397
Eric Paris2a7dba32011-02-01 11:05:39 -05001398 rc = jfs_init_security(tid, ip, dir, &dentry->d_name);
Dave Kleikamp1d15b10f2005-09-01 09:05:39 -05001399 if (rc) {
1400 txAbort(tid, 0);
1401 goto out3;
1402 }
1403
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001404 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE))) {
1405 txAbort(tid, 0);
1406 goto out3;
1407 }
1408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 tblk = tid_to_tblock(tid);
1410 tblk->xflag |= COMMIT_CREATE;
1411 tblk->ino = ip->i_ino;
1412 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
1413
1414 ino = ip->i_ino;
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001415 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack))) {
1416 txAbort(tid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 goto out3;
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 ip->i_op = &jfs_file_inode_operations;
1421 jfs_ip->dev = new_encode_dev(rdev);
1422 init_special_inode(ip, ip->i_mode, rdev);
1423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 mark_inode_dirty(ip);
1425
Deepa Dinamani078cd822016-09-14 07:48:04 -07001426 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428 mark_inode_dirty(dir);
1429
1430 iplist[0] = dir;
1431 iplist[1] = ip;
1432 rc = txCommit(tid, 2, iplist, 0);
1433
1434 out3:
1435 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -06001436 mutex_unlock(&JFS_IP(ip)->commit_mutex);
1437 mutex_unlock(&JFS_IP(dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 if (rc) {
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001439 free_ea_wmap(ip);
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02001440 clear_nlink(ip);
Al Viroa6cbedf2018-06-29 11:59:37 -04001441 discard_new_inode(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001442 } else {
Al Viro1e2e5472018-05-04 08:23:01 -04001443 d_instantiate_new(dentry, ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 out1:
1447 free_UCSname(&dname);
1448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 out:
1450 jfs_info("jfs_mknod: returning %d", rc);
1451 return rc;
1452}
1453
Al Viro00cd8dd2012-06-10 17:13:09 -04001454static struct dentry *jfs_lookup(struct inode *dip, struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
1456 struct btstack btstack;
1457 ino_t inum;
1458 struct inode *ip;
1459 struct component_name key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 int rc;
1461
Al Viroa4555892014-10-21 20:11:25 -04001462 jfs_info("jfs_lookup: name = %pd", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Al Viro79ac5a42011-07-17 10:17:46 -04001464 if ((rc = get_UCSname(&key, dentry)))
1465 return ERR_PTR(rc);
1466 rc = dtSearch(dip, &key, &inum, &btstack, JFS_LOOKUP);
1467 free_UCSname(&key);
1468 if (rc == -ENOENT) {
1469 ip = NULL;
1470 } else if (rc) {
1471 jfs_err("jfs_lookup: dtSearch returned %d", rc);
1472 ip = ERR_PTR(rc);
1473 } else {
1474 ip = jfs_iget(dip->i_sb, inum);
1475 if (IS_ERR(ip))
1476 jfs_err("jfs_lookup: iget failed on inum %d", (uint)inum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 }
1478
Al Viro94b77bd2010-12-18 10:59:31 -05001479 return d_splice_alias(ip, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480}
1481
Christoph Hellwigd425de72007-10-21 16:42:09 -07001482static struct inode *jfs_nfs_get_inode(struct super_block *sb,
1483 u64 ino, u32 generation)
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001484{
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001485 struct inode *inode;
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001486
1487 if (ino == 0)
1488 return ERR_PTR(-ESTALE);
David Howellseab1df72008-02-07 00:15:43 -08001489 inode = jfs_iget(sb, ino);
1490 if (IS_ERR(inode))
1491 return ERR_CAST(inode);
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001492
David Howellseab1df72008-02-07 00:15:43 -08001493 if (generation && inode->i_generation != generation) {
Christoph Hellwigd425de72007-10-21 16:42:09 -07001494 iput(inode);
1495 return ERR_PTR(-ESTALE);
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001496 }
1497
Christoph Hellwigd425de72007-10-21 16:42:09 -07001498 return inode;
1499}
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001500
Christoph Hellwigd425de72007-10-21 16:42:09 -07001501struct dentry *jfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
1502 int fh_len, int fh_type)
1503{
1504 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1505 jfs_nfs_get_inode);
1506}
1507
1508struct dentry *jfs_fh_to_parent(struct super_block *sb, struct fid *fid,
1509 int fh_len, int fh_type)
1510{
1511 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1512 jfs_nfs_get_inode);
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001513}
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515struct dentry *jfs_get_parent(struct dentry *dentry)
1516{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 unsigned long parent_ino;
1518
1519 parent_ino =
David Howells2b0143b2015-03-17 22:25:59 +00001520 le32_to_cpu(JFS_IP(d_inode(dentry))->i_dtroot.header.idotdot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Al Virofc640052016-04-10 01:33:30 -04001522 return d_obtain_alias(jfs_iget(dentry->d_sb, parent_ino));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001525const struct inode_operations jfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 .create = jfs_create,
1527 .lookup = jfs_lookup,
1528 .link = jfs_link,
1529 .unlink = jfs_unlink,
1530 .symlink = jfs_symlink,
1531 .mkdir = jfs_mkdir,
1532 .rmdir = jfs_rmdir,
1533 .mknod = jfs_mknod,
1534 .rename = jfs_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 .listxattr = jfs_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 .setattr = jfs_setattr,
Christoph Hellwig759bfee2010-03-03 09:05:02 -05001537#ifdef CONFIG_JFS_POSIX_ACL
Christoph Hellwig4e34e712011-07-23 17:37:31 +02001538 .get_acl = jfs_get_acl,
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -08001539 .set_acl = jfs_set_acl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540#endif
1541};
1542
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001543const struct file_operations jfs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 .read = generic_read_dir,
Al Viro070a0eb2013-05-17 17:00:34 -04001545 .iterate = jfs_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 .fsync = jfs_fsync,
Andi Kleenbaab81f2008-01-27 16:58:51 -06001547 .unlocked_ioctl = jfs_ioctl,
Andi Kleenef1fc2f2008-01-27 17:02:02 -06001548#ifdef CONFIG_COMPAT
1549 .compat_ioctl = jfs_compat_ioctl,
1550#endif
Christoph Hellwig3222a3e2008-09-03 21:53:01 +02001551 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552};
1553
Linus Torvaldsda53be12013-05-21 15:22:44 -07001554static int jfs_ci_hash(const struct dentry *dir, struct qstr *this)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
1556 unsigned long hash;
1557 int i;
1558
Linus Torvalds8387ff22016-06-10 07:51:30 -07001559 hash = init_name_hash(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 for (i=0; i < this->len; i++)
1561 hash = partial_name_hash(tolower(this->name[i]), hash);
1562 this->hash = end_name_hash(hash);
1563
1564 return 0;
1565}
1566
Al Viro6fa67e72016-07-31 16:37:25 -04001567static int jfs_ci_compare(const struct dentry *dentry,
Nick Piggin621e1552011-01-07 17:49:27 +11001568 unsigned int len, const char *str, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
1570 int i, result = 1;
1571
Nick Piggin621e1552011-01-07 17:49:27 +11001572 if (len != name->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 goto out;
Nick Piggin621e1552011-01-07 17:49:27 +11001574 for (i=0; i < len; i++) {
1575 if (tolower(str[i]) != tolower(name->name[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 goto out;
1577 }
1578 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579out:
1580 return result;
1581}
1582
Al Viro0b728e12012-06-10 16:03:43 -04001583static int jfs_ci_revalidate(struct dentry *dentry, unsigned int flags)
Nick Piggin2bc334d2011-01-07 17:49:25 +11001584{
1585 /*
1586 * This is not negative dentry. Always valid.
1587 *
1588 * Note, rename() to existing directory entry will have ->d_inode,
1589 * and will use existing name which isn't specified name by user.
1590 *
1591 * We may be able to drop this positive dentry here. But dropping
1592 * positive dentry isn't good idea. So it's unsupported like
1593 * rename("filename", "FILENAME") for now.
1594 */
David Howells2b0143b2015-03-17 22:25:59 +00001595 if (d_really_is_positive(dentry))
Nick Piggin2bc334d2011-01-07 17:49:25 +11001596 return 1;
1597
1598 /*
1599 * This may be nfsd (or something), anyway, we can't see the
1600 * intent of this. So, since this can be for creation, drop it.
1601 */
Al Viro0b728e12012-06-10 16:03:43 -04001602 if (!flags)
Nick Piggin2bc334d2011-01-07 17:49:25 +11001603 return 0;
1604
1605 /*
1606 * Drop the negative dentry, in order to make sure to use the
1607 * case sensitive name which is specified by user if this is
1608 * for creation.
1609 */
Al Viro0b728e12012-06-10 16:03:43 -04001610 if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
Al Viro407938e2011-06-25 21:37:18 -04001611 return 0;
Nick Piggin2bc334d2011-01-07 17:49:25 +11001612 return 1;
1613}
1614
Al Viroad28b4e2009-02-20 06:00:49 +00001615const struct dentry_operations jfs_ci_dentry_operations =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
1617 .d_hash = jfs_ci_hash,
1618 .d_compare = jfs_ci_compare,
Nick Piggin2bc334d2011-01-07 17:49:25 +11001619 .d_revalidate = jfs_ci_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620};