blob: 56c3fcbfe80ed0b69156bcab2f981b4d322b0aba [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);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -0600178 unlock_new_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 iput(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -0600180 } else {
Al Viro2d2d3f12018-05-04 08:23:01 -0400181 d_instantiate_new(dentry, ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -0600182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 out2:
185 free_UCSname(&dname);
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 out1:
188
189 jfs_info("jfs_create: rc:%d", rc);
190 return rc;
191}
192
193
194/*
195 * NAME: jfs_mkdir(dip, dentry, mode)
196 *
197 * FUNCTION: create a child directory in the parent directory <dip>
198 * with name = <from dentry> and mode = <mode>
199 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500200 * PARAMETER: dip - parent directory vnode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 * dentry - dentry of child directory
202 * mode - create mode (rwxrwxrwx).
203 *
204 * RETURN: Errors from subroutines
205 *
206 * note:
207 * EACCESS: user needs search+write permission on the parent directory
208 */
Al Viro18bb1db2011-07-26 01:41:39 -0400209static int jfs_mkdir(struct inode *dip, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 int rc = 0;
212 tid_t tid; /* transaction id */
213 struct inode *ip = NULL; /* child directory inode */
214 ino_t ino;
215 struct component_name dname; /* child directory name */
216 struct btstack btstack;
217 struct inode *iplist[2];
218 struct tblock *tblk;
219
Al Viroa4555892014-10-21 20:11:25 -0400220 jfs_info("jfs_mkdir: dip:0x%p name:%pd", dip, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Dave Kleikampacc84b02015-07-15 13:53:19 -0500222 rc = dquot_initialize(dip);
223 if (rc)
224 goto out1;
Christoph Hellwig907f4552010-03-03 09:05:06 -0500225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 /*
227 * search parent directory for entry/freespace
228 * (dtSearch() returns parent directory page pinned)
229 */
230 if ((rc = get_UCSname(&dname, dentry)))
231 goto out1;
232
233 /*
234 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
235 * block there while holding dtree page, so we allocate the inode &
236 * begin the transaction before we search the directory.
237 */
238 ip = ialloc(dip, S_IFDIR | mode);
Akinobu Mita087387f2006-09-14 09:22:38 -0500239 if (IS_ERR(ip)) {
240 rc = PTR_ERR(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 goto out2;
242 }
243
244 tid = txBegin(dip->i_sb, 0);
245
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600246 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
247 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500249 rc = jfs_init_acl(tid, ip, dip);
250 if (rc)
251 goto out3;
252
Eric Paris2a7dba32011-02-01 11:05:39 -0500253 rc = jfs_init_security(tid, ip, dip, &dentry->d_name);
Dave Kleikamp1d15b10f2005-09-01 09:05:39 -0500254 if (rc) {
255 txAbort(tid, 0);
256 goto out3;
257 }
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
260 jfs_err("jfs_mkdir: dtSearch returned %d", rc);
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500261 txAbort(tid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 goto out3;
263 }
264
265 tblk = tid_to_tblock(tid);
266 tblk->xflag |= COMMIT_CREATE;
267 tblk->ino = ip->i_ino;
268 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
269
270 iplist[0] = dip;
271 iplist[1] = ip;
272
273 /*
274 * initialize the child directory in-line in inode
275 */
276 dtInitRoot(tid, ip, dip->i_ino);
277
278 /*
279 * create entry in parent directory for child directory
280 * (dtInsert() releases parent directory page)
281 */
282 ino = ip->i_ino;
283 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
284 if (rc == -EIO) {
285 jfs_err("jfs_mkdir: dtInsert returned -EIO");
286 txAbort(tid, 1); /* Marks Filesystem dirty */
287 } else
288 txAbort(tid, 0); /* Filesystem full */
289 goto out3;
290 }
291
Miklos Szeredibfe86842011-10-28 14:13:29 +0200292 set_nlink(ip, 2); /* for '.' */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 ip->i_op = &jfs_dir_inode_operations;
294 ip->i_fop = &jfs_dir_operations;
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 mark_inode_dirty(ip);
297
298 /* update parent directory inode */
Dave Hansend8c76e62006-09-30 23:29:04 -0700299 inc_nlink(dip); /* for '..' from child directory */
Deepa Dinamani078cd822016-09-14 07:48:04 -0700300 dip->i_ctime = dip->i_mtime = current_time(dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 mark_inode_dirty(dip);
302
303 rc = txCommit(tid, 2, &iplist[0], 0);
304
305 out3:
306 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600307 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500308 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (rc) {
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500310 free_ea_wmap(ip);
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200311 clear_nlink(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -0600312 unlock_new_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 iput(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -0600314 } else {
Al Viro2d2d3f12018-05-04 08:23:01 -0400315 d_instantiate_new(dentry, ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -0600316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 out2:
319 free_UCSname(&dname);
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 out1:
323
324 jfs_info("jfs_mkdir: rc:%d", rc);
325 return rc;
326}
327
328/*
329 * NAME: jfs_rmdir(dip, dentry)
330 *
331 * FUNCTION: remove a link to child directory
332 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500333 * PARAMETER: dip - parent inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 * dentry - child directory dentry
335 *
336 * RETURN: -EINVAL - if name is . or ..
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500337 * -EINVAL - if . or .. exist but are invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 * errors from subroutines
339 *
340 * note:
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500341 * if other threads have the directory open when the last link
342 * is removed, the "." and ".." entries, if present, are removed before
343 * rmdir() returns and no new entries may be created in the directory,
344 * but the directory is not removed until the last reference to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 * the directory is released (cf.unlink() of regular file).
346 */
347static int jfs_rmdir(struct inode *dip, struct dentry *dentry)
348{
349 int rc;
350 tid_t tid; /* transaction id */
David Howells2b0143b2015-03-17 22:25:59 +0000351 struct inode *ip = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 ino_t ino;
353 struct component_name dname;
354 struct inode *iplist[2];
355 struct tblock *tblk;
356
Al Viroa4555892014-10-21 20:11:25 -0400357 jfs_info("jfs_rmdir: dip:0x%p name:%pd", dip, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 /* Init inode for quota operations. */
Dave Kleikampacc84b02015-07-15 13:53:19 -0500360 rc = dquot_initialize(dip);
361 if (rc)
362 goto out;
363 rc = dquot_initialize(ip);
364 if (rc)
365 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 /* directory must be empty to be removed */
368 if (!dtEmpty(ip)) {
369 rc = -ENOTEMPTY;
370 goto out;
371 }
372
373 if ((rc = get_UCSname(&dname, dentry))) {
374 goto out;
375 }
376
377 tid = txBegin(dip->i_sb, 0);
378
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600379 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
380 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 iplist[0] = dip;
383 iplist[1] = ip;
384
385 tblk = tid_to_tblock(tid);
386 tblk->xflag |= COMMIT_DELETE;
387 tblk->u.ip = ip;
388
389 /*
390 * delete the entry of target directory from parent directory
391 */
392 ino = ip->i_ino;
393 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
394 jfs_err("jfs_rmdir: dtDelete returned %d", rc);
395 if (rc == -EIO)
396 txAbort(tid, 1);
397 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600398 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500399 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 goto out2;
402 }
403
404 /* update parent directory's link count corresponding
405 * to ".." entry of the target directory deleted
406 */
Deepa Dinamani078cd822016-09-14 07:48:04 -0700407 dip->i_ctime = dip->i_mtime = current_time(dip);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700408 inode_dec_link_count(dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 /*
411 * OS/2 could have created EA and/or ACL
412 */
413 /* free EA from both persistent and working map */
414 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
415 /* free EA pages */
416 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
417 }
418 JFS_IP(ip)->ea.flag = 0;
419
420 /* free ACL from both persistent and working map */
421 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
422 /* free ACL pages */
423 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
424 }
425 JFS_IP(ip)->acl.flag = 0;
426
427 /* mark the target directory as deleted */
Dave Hansence71ec32006-09-30 23:29:06 -0700428 clear_nlink(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 mark_inode_dirty(ip);
430
431 rc = txCommit(tid, 2, &iplist[0], 0);
432
433 txEnd(tid);
434
Ingo Molnar1de87442006-01-24 15:22:50 -0600435 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500436 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 /*
439 * Truncating the directory index table is not guaranteed. It
440 * may need to be done iteratively
441 */
442 if (test_cflag(COMMIT_Stale, dip)) {
443 if (dip->i_size > 1)
444 jfs_truncate_nolock(dip, 0);
445
446 clear_cflag(COMMIT_Stale, dip);
447 }
448
449 out2:
450 free_UCSname(&dname);
451
452 out:
453 jfs_info("jfs_rmdir: rc:%d", rc);
454 return rc;
455}
456
457/*
458 * NAME: jfs_unlink(dip, dentry)
459 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500460 * FUNCTION: remove a link to object <vp> named by <name>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 * from parent directory <dvp>
462 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500463 * PARAMETER: dip - inode of parent directory
464 * dentry - dentry of object to be removed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 *
466 * RETURN: errors from subroutines
467 *
468 * note:
469 * temporary file: if one or more processes have the file open
470 * when the last link is removed, the link will be removed before
471 * unlink() returns, but the removal of the file contents will be
472 * postponed until all references to the files are closed.
473 *
474 * JFS does NOT support unlink() on directories.
475 *
476 */
477static int jfs_unlink(struct inode *dip, struct dentry *dentry)
478{
479 int rc;
480 tid_t tid; /* transaction id */
David Howells2b0143b2015-03-17 22:25:59 +0000481 struct inode *ip = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 ino_t ino;
483 struct component_name dname; /* object name */
484 struct inode *iplist[2];
485 struct tblock *tblk;
486 s64 new_size = 0;
487 int commit_flag;
488
Al Viroa4555892014-10-21 20:11:25 -0400489 jfs_info("jfs_unlink: dip:0x%p name:%pd", dip, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 /* Init inode for quota operations. */
Dave Kleikampacc84b02015-07-15 13:53:19 -0500492 rc = dquot_initialize(dip);
493 if (rc)
494 goto out;
495 rc = dquot_initialize(ip);
496 if (rc)
497 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 if ((rc = get_UCSname(&dname, dentry)))
500 goto out;
501
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600502 IWRITE_LOCK(ip, RDWRLOCK_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 tid = txBegin(dip->i_sb, 0);
505
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600506 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
507 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 iplist[0] = dip;
510 iplist[1] = ip;
511
512 /*
513 * delete the entry of target file from parent directory
514 */
515 ino = ip->i_ino;
516 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
517 jfs_err("jfs_unlink: dtDelete returned %d", rc);
518 if (rc == -EIO)
519 txAbort(tid, 1); /* Marks FS Dirty */
520 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600521 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500522 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 IWRITE_UNLOCK(ip);
524 goto out1;
525 }
526
527 ASSERT(ip->i_nlink);
528
Deepa Dinamani078cd822016-09-14 07:48:04 -0700529 ip->i_ctime = dip->i_ctime = dip->i_mtime = current_time(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 mark_inode_dirty(dip);
531
532 /* update target's inode */
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700533 inode_dec_link_count(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 /*
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500536 * commit zero link count object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 */
538 if (ip->i_nlink == 0) {
539 assert(!test_cflag(COMMIT_Nolink, ip));
540 /* free block resources */
541 if ((new_size = commitZeroLink(tid, ip)) < 0) {
542 txAbort(tid, 1); /* Marks FS Dirty */
543 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600544 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500545 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 IWRITE_UNLOCK(ip);
547 rc = new_size;
548 goto out1;
549 }
550 tblk = tid_to_tblock(tid);
551 tblk->xflag |= COMMIT_DELETE;
552 tblk->u.ip = ip;
553 }
554
555 /*
556 * Incomplete truncate of file data can
557 * result in timing problems unless we synchronously commit the
558 * transaction.
559 */
560 if (new_size)
561 commit_flag = COMMIT_SYNC;
562 else
563 commit_flag = 0;
564
565 /*
566 * If xtTruncate was incomplete, commit synchronously to avoid
567 * timing complications
568 */
569 rc = txCommit(tid, 2, &iplist[0], commit_flag);
570
571 txEnd(tid);
572
Ingo Molnar1de87442006-01-24 15:22:50 -0600573 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500574 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 while (new_size && (rc == 0)) {
577 tid = txBegin(dip->i_sb, 0);
Ingo Molnar1de87442006-01-24 15:22:50 -0600578 mutex_lock(&JFS_IP(ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 new_size = xtTruncate_pmap(tid, ip, new_size);
580 if (new_size < 0) {
581 txAbort(tid, 1); /* Marks FS Dirty */
582 rc = new_size;
583 } else
584 rc = txCommit(tid, 2, &iplist[0], COMMIT_SYNC);
585 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600586 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
588
589 if (ip->i_nlink == 0)
590 set_cflag(COMMIT_Nolink, ip);
591
592 IWRITE_UNLOCK(ip);
593
594 /*
595 * Truncating the directory index table is not guaranteed. It
596 * may need to be done iteratively
597 */
598 if (test_cflag(COMMIT_Stale, dip)) {
599 if (dip->i_size > 1)
600 jfs_truncate_nolock(dip, 0);
601
602 clear_cflag(COMMIT_Stale, dip);
603 }
604
605 out1:
606 free_UCSname(&dname);
607 out:
608 jfs_info("jfs_unlink: rc:%d", rc);
609 return rc;
610}
611
612/*
613 * NAME: commitZeroLink()
614 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500615 * FUNCTION: for non-directory, called by jfs_remove(),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 * truncate a regular file, directory or symbolic
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500617 * link to zero length. return 0 if type is not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 * one of these.
619 *
620 * if the file is currently associated with a VM segment
621 * only permanent disk and inode map resources are freed,
622 * and neither the inode nor indirect blocks are modified
623 * so that the resources can be later freed in the work
624 * map by ctrunc1.
625 * if there is no VM segment on entry, the resources are
626 * freed in both work and permanent map.
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500627 * (? for temporary file - memory object is cached even
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 * after no reference:
629 * reference count > 0 - )
630 *
631 * PARAMETERS: cd - pointer to commit data structure.
632 * current inode is the one to truncate.
633 *
634 * RETURN: Errors from subroutines
635 */
636static s64 commitZeroLink(tid_t tid, struct inode *ip)
637{
638 int filetype;
639 struct tblock *tblk;
640
641 jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid, ip);
642
643 filetype = ip->i_mode & S_IFMT;
644 switch (filetype) {
645 case S_IFREG:
646 break;
647 case S_IFLNK:
648 /* fast symbolic link */
649 if (ip->i_size < IDATASIZE) {
650 ip->i_size = 0;
651 return 0;
652 }
653 break;
654 default:
655 assert(filetype != S_IFDIR);
656 return 0;
657 }
658
659 set_cflag(COMMIT_Freewmap, ip);
660
661 /* mark transaction of block map update type */
662 tblk = tid_to_tblock(tid);
663 tblk->xflag |= COMMIT_PMAP;
664
665 /*
666 * free EA
667 */
668 if (JFS_IP(ip)->ea.flag & DXD_EXTENT)
669 /* acquire maplock on EA to be freed from block map */
670 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
671
672 /*
673 * free ACL
674 */
675 if (JFS_IP(ip)->acl.flag & DXD_EXTENT)
676 /* acquire maplock on EA to be freed from block map */
677 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
678
679 /*
680 * free xtree/data (truncate to zero length):
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500681 * free xtree/data pages from cache if COMMIT_PWMAP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 * free xtree/data blocks from persistent block map, and
683 * free xtree/data blocks from working block map if COMMIT_PWMAP;
684 */
685 if (ip->i_size)
686 return xtTruncate_pmap(tid, ip, 0);
687
688 return 0;
689}
690
691
692/*
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500693 * NAME: jfs_free_zero_link()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500695 * FUNCTION: for non-directory, called by iClose(),
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500696 * free resources of a file from cache and WORKING map
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 * for a file previously committed with zero link count
698 * while associated with a pager object,
699 *
700 * PARAMETER: ip - pointer to inode of file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 */
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500702void jfs_free_zero_link(struct inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 int type;
705
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500706 jfs_info("jfs_free_zero_link: ip = 0x%p", ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 /* return if not reg or symbolic link or if size is
709 * already ok.
710 */
711 type = ip->i_mode & S_IFMT;
712
713 switch (type) {
714 case S_IFREG:
715 break;
716 case S_IFLNK:
717 /* if its contained in inode nothing to do */
718 if (ip->i_size < IDATASIZE)
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500719 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 break;
721 default:
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500722 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
724
725 /*
726 * free EA
727 */
728 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
729 s64 xaddr = addressDXD(&JFS_IP(ip)->ea);
730 int xlen = lengthDXD(&JFS_IP(ip)->ea);
731 struct maplock maplock; /* maplock for COMMIT_WMAP */
732 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
733
734 /* free EA pages from cache */
735 invalidate_dxd_metapages(ip, JFS_IP(ip)->ea);
736
737 /* free EA extent from working block map */
738 maplock.index = 1;
739 pxdlock = (struct pxd_lock *) & maplock;
740 pxdlock->flag = mlckFREEPXD;
741 PXDaddress(&pxdlock->pxd, xaddr);
742 PXDlength(&pxdlock->pxd, xlen);
743 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
744 }
745
746 /*
747 * free ACL
748 */
749 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
750 s64 xaddr = addressDXD(&JFS_IP(ip)->acl);
751 int xlen = lengthDXD(&JFS_IP(ip)->acl);
752 struct maplock maplock; /* maplock for COMMIT_WMAP */
753 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
754
755 invalidate_dxd_metapages(ip, JFS_IP(ip)->acl);
756
757 /* free ACL extent from working block map */
758 maplock.index = 1;
759 pxdlock = (struct pxd_lock *) & maplock;
760 pxdlock->flag = mlckFREEPXD;
761 PXDaddress(&pxdlock->pxd, xaddr);
762 PXDlength(&pxdlock->pxd, xlen);
763 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
764 }
765
766 /*
767 * free xtree/data (truncate to zero length):
768 * free xtree/data pages from cache, and
769 * free xtree/data blocks from working block map;
770 */
771 if (ip->i_size)
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500772 xtTruncate(0, ip, 0, COMMIT_WMAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
775/*
776 * NAME: jfs_link(vp, dvp, name, crp)
777 *
778 * FUNCTION: create a link to <vp> by the name = <name>
779 * in the parent directory <dvp>
780 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500781 * PARAMETER: vp - target object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 * dvp - parent directory of new link
783 * name - name of new link to target object
784 * crp - credential
785 *
786 * RETURN: Errors from subroutines
787 *
788 * note:
789 * JFS does NOT support link() on directories (to prevent circular
790 * path in the directory hierarchy);
791 * EPERM: the target object is a directory, and either the caller
792 * does not have appropriate privileges or the implementation prohibits
793 * using link() on directories [XPG4.2].
794 *
795 * JFS does NOT support links between file systems:
796 * EXDEV: target object and new link are on different file systems and
797 * implementation does not support links between file systems [XPG4.2].
798 */
799static int jfs_link(struct dentry *old_dentry,
800 struct inode *dir, struct dentry *dentry)
801{
802 int rc;
803 tid_t tid;
David Howells2b0143b2015-03-17 22:25:59 +0000804 struct inode *ip = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 ino_t ino;
806 struct component_name dname;
807 struct btstack btstack;
808 struct inode *iplist[2];
809
Al Viroa4555892014-10-21 20:11:25 -0400810 jfs_info("jfs_link: %pd %pd", old_dentry, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Dave Kleikampacc84b02015-07-15 13:53:19 -0500812 rc = dquot_initialize(dir);
813 if (rc)
814 goto out;
Christoph Hellwig907f4552010-03-03 09:05:06 -0500815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 tid = txBegin(ip->i_sb, 0);
817
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600818 mutex_lock_nested(&JFS_IP(dir)->commit_mutex, COMMIT_MUTEX_PARENT);
819 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 /*
822 * scan parent directory for entry/freespace
823 */
824 if ((rc = get_UCSname(&dname, dentry)))
Dave Kleikampacc84b02015-07-15 13:53:19 -0500825 goto out_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
828 goto free_dname;
829
830 /*
831 * create entry for new link in parent directory
832 */
833 ino = ip->i_ino;
834 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
835 goto free_dname;
836
837 /* update object inode */
Dave Hansend8c76e62006-09-30 23:29:04 -0700838 inc_nlink(ip); /* for new link */
Deepa Dinamani078cd822016-09-14 07:48:04 -0700839 ip->i_ctime = current_time(ip);
840 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 mark_inode_dirty(dir);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400842 ihold(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 iplist[0] = ip;
845 iplist[1] = dir;
846 rc = txCommit(tid, 2, &iplist[0], 0);
847
848 if (rc) {
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200849 drop_nlink(ip); /* never instantiated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 iput(ip);
851 } else
852 d_instantiate(dentry, ip);
853
854 free_dname:
855 free_UCSname(&dname);
856
Dave Kleikampacc84b02015-07-15 13:53:19 -0500857 out_tx:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 txEnd(tid);
859
Ingo Molnar1de87442006-01-24 15:22:50 -0600860 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500861 mutex_unlock(&JFS_IP(dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Dave Kleikampacc84b02015-07-15 13:53:19 -0500863 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 jfs_info("jfs_link: rc:%d", rc);
865 return rc;
866}
867
868/*
869 * NAME: jfs_symlink(dip, dentry, name)
870 *
871 * FUNCTION: creates a symbolic link to <symlink> by name <name>
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500872 * in directory <dip>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500874 * PARAMETER: dip - parent directory vnode
875 * dentry - dentry of symbolic link
876 * name - the path name of the existing object
877 * that will be the source of the link
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 *
879 * RETURN: errors from subroutines
880 *
881 * note:
882 * ENAMETOOLONG: pathname resolution of a symbolic link produced
883 * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
884*/
885
886static int jfs_symlink(struct inode *dip, struct dentry *dentry,
887 const char *name)
888{
889 int rc;
890 tid_t tid;
891 ino_t ino = 0;
892 struct component_name dname;
893 int ssize; /* source pathname size */
894 struct btstack btstack;
David Howells2b0143b2015-03-17 22:25:59 +0000895 struct inode *ip = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 s64 xlen = 0;
897 int bmask = 0, xsize;
Dave Kleikamp3c2c2262011-06-20 13:00:27 -0500898 s64 xaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 struct metapage *mp;
900 struct super_block *sb;
901 struct tblock *tblk;
902
903 struct inode *iplist[2];
904
905 jfs_info("jfs_symlink: dip:0x%p name:%s", dip, name);
906
Dave Kleikampacc84b02015-07-15 13:53:19 -0500907 rc = dquot_initialize(dip);
908 if (rc)
909 goto out1;
Christoph Hellwig907f4552010-03-03 09:05:06 -0500910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 ssize = strlen(name) + 1;
912
913 /*
914 * search parent directory for entry/freespace
915 * (dtSearch() returns parent directory page pinned)
916 */
917
918 if ((rc = get_UCSname(&dname, dentry)))
919 goto out1;
920
921 /*
922 * allocate on-disk/in-memory inode for symbolic link:
923 * (iAlloc() returns new, locked inode)
924 */
925 ip = ialloc(dip, S_IFLNK | 0777);
Akinobu Mita087387f2006-09-14 09:22:38 -0500926 if (IS_ERR(ip)) {
927 rc = PTR_ERR(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 goto out2;
929 }
930
931 tid = txBegin(dip->i_sb, 0);
932
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600933 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
934 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Eric Paris2a7dba32011-02-01 11:05:39 -0500936 rc = jfs_init_security(tid, ip, dip, &dentry->d_name);
Dave Kleikamp1d15b10f2005-09-01 09:05:39 -0500937 if (rc)
938 goto out3;
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 tblk = tid_to_tblock(tid);
941 tblk->xflag |= COMMIT_CREATE;
942 tblk->ino = ip->i_ino;
943 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
944
945 /* fix symlink access permission
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500946 * (dir_create() ANDs in the u.u_cmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 * but symlinks really need to be 777 access)
948 */
949 ip->i_mode |= 0777;
950
951 /*
952 * write symbolic link target path name
953 */
954 xtInitRoot(tid, ip);
955
956 /*
957 * write source path name inline in on-disk inode (fast symbolic link)
958 */
959
960 if (ssize <= IDATASIZE) {
Dmitry Monakhovc7f2e1f2010-04-16 08:05:50 -0500961 ip->i_op = &jfs_fast_symlink_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Al Viroad476fe2015-05-02 10:41:20 -0400963 ip->i_link = JFS_IP(ip)->i_inline;
964 memcpy(ip->i_link, name, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 ip->i_size = ssize - 1;
966
967 /*
968 * if symlink is > 128 bytes, we don't have the space to
969 * store inline extended attributes
970 */
971 if (ssize > sizeof (JFS_IP(ip)->i_inline))
972 JFS_IP(ip)->mode2 &= ~INLINEEA;
973
974 jfs_info("jfs_symlink: fast symlink added ssize:%d name:%s ",
975 ssize, name);
976 }
977 /*
978 * write source path name in a single extent
979 */
980 else {
981 jfs_info("jfs_symlink: allocate extent ip:0x%p", ip);
982
Dmitry Monakhovc7f2e1f2010-04-16 08:05:50 -0500983 ip->i_op = &jfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -0500984 inode_nohighmem(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 ip->i_mapping->a_ops = &jfs_aops;
986
987 /*
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500988 * even though the data of symlink object (source
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 * path name) is treated as non-journaled user data,
990 * it is read/written thru buffer cache for performance.
991 */
992 sb = ip->i_sb;
993 bmask = JFS_SBI(sb)->bsize - 1;
994 xsize = (ssize + bmask) & ~bmask;
995 xaddr = 0;
996 xlen = xsize >> JFS_SBI(sb)->l2bsize;
997 if ((rc = xtInsert(tid, ip, 0, 0, xlen, &xaddr, 0))) {
998 txAbort(tid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 goto out3;
1000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 ip->i_size = ssize - 1;
1002 while (ssize) {
1003 /* This is kind of silly since PATH_MAX == 4K */
1004 int copy_size = min(ssize, PSIZE);
1005
1006 mp = get_metapage(ip, xaddr, PSIZE, 1);
1007
1008 if (mp == NULL) {
1009 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
1010 rc = -EIO;
1011 txAbort(tid, 0);
1012 goto out3;
1013 }
1014 memcpy(mp->data, name, copy_size);
1015 flush_metapage(mp);
1016 ssize -= copy_size;
1017 name += copy_size;
1018 xaddr += JFS_SBI(sb)->nbperpage;
1019 }
1020 }
1021
1022 /*
1023 * create entry for symbolic link in parent directory
1024 */
1025 rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE);
1026 if (rc == 0) {
1027 ino = ip->i_ino;
1028 rc = dtInsert(tid, dip, &dname, &ino, &btstack);
1029 }
1030 if (rc) {
1031 if (xlen)
1032 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
1033 txAbort(tid, 0);
1034 /* discard new inode */
1035 goto out3;
1036 }
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 mark_inode_dirty(ip);
1039
Deepa Dinamani078cd822016-09-14 07:48:04 -07001040 dip->i_ctime = dip->i_mtime = current_time(dip);
Dave Kleikamp988a6492005-10-31 16:53:04 -06001041 mark_inode_dirty(dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 /*
1043 * commit update of parent directory and link object
1044 */
1045
1046 iplist[0] = dip;
1047 iplist[1] = ip;
1048 rc = txCommit(tid, 2, &iplist[0], 0);
1049
1050 out3:
1051 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -06001052 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -05001053 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (rc) {
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001055 free_ea_wmap(ip);
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02001056 clear_nlink(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001057 unlock_new_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 iput(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001059 } else {
Al Viro2d2d3f12018-05-04 08:23:01 -04001060 d_instantiate_new(dentry, ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 out2:
1064 free_UCSname(&dname);
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 out1:
1067 jfs_info("jfs_symlink: rc:%d", rc);
1068 return rc;
1069}
1070
1071
1072/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001073 * NAME: jfs_rename
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001075 * FUNCTION: rename a file or directory
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 */
1077static int jfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredif03b8ad2016-09-27 11:03:57 +02001078 struct inode *new_dir, struct dentry *new_dentry,
1079 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
1081 struct btstack btstack;
1082 ino_t ino;
1083 struct component_name new_dname;
1084 struct inode *new_ip;
1085 struct component_name old_dname;
1086 struct inode *old_ip;
1087 int rc;
1088 tid_t tid;
1089 struct tlock *tlck;
1090 struct dt_lock *dtlck;
1091 struct lv *lv;
1092 int ipcount;
1093 struct inode *iplist[4];
1094 struct tblock *tblk;
1095 s64 new_size = 0;
1096 int commit_flag;
1097
Miklos Szeredif03b8ad2016-09-27 11:03:57 +02001098 if (flags & ~RENAME_NOREPLACE)
1099 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Al Viroa4555892014-10-21 20:11:25 -04001101 jfs_info("jfs_rename: %pd %pd", old_dentry, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Dave Kleikampacc84b02015-07-15 13:53:19 -05001103 rc = dquot_initialize(old_dir);
1104 if (rc)
1105 goto out1;
1106 rc = dquot_initialize(new_dir);
1107 if (rc)
1108 goto out1;
Christoph Hellwig907f4552010-03-03 09:05:06 -05001109
David Howells2b0143b2015-03-17 22:25:59 +00001110 old_ip = d_inode(old_dentry);
1111 new_ip = d_inode(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113 if ((rc = get_UCSname(&old_dname, old_dentry)))
1114 goto out1;
1115
1116 if ((rc = get_UCSname(&new_dname, new_dentry)))
1117 goto out2;
1118
1119 /*
1120 * Make sure source inode number is what we think it is
1121 */
1122 rc = dtSearch(old_dir, &old_dname, &ino, &btstack, JFS_LOOKUP);
1123 if (rc || (ino != old_ip->i_ino)) {
1124 rc = -ENOENT;
1125 goto out3;
1126 }
1127
1128 /*
1129 * Make sure dest inode number (if any) is what we think it is
1130 */
1131 rc = dtSearch(new_dir, &new_dname, &ino, &btstack, JFS_LOOKUP);
Joe Perches09aaa742007-11-13 22:16:08 -06001132 if (!rc) {
Dave Kleikampda8a41d2007-11-13 22:25:41 -06001133 if ((!new_ip) || (ino != new_ip->i_ino)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 rc = -ESTALE;
1135 goto out3;
1136 }
1137 } else if (rc != -ENOENT)
1138 goto out3;
1139 else if (new_ip) {
1140 /* no entry exists, but one was expected */
1141 rc = -ESTALE;
1142 goto out3;
1143 }
1144
1145 if (S_ISDIR(old_ip->i_mode)) {
1146 if (new_ip) {
1147 if (!dtEmpty(new_ip)) {
1148 rc = -ENOTEMPTY;
1149 goto out3;
1150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
1152 } else if (new_ip) {
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001153 IWRITE_LOCK(new_ip, RDWRLOCK_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 /* Init inode for quota operations. */
Dave Kleikampacc84b02015-07-15 13:53:19 -05001155 rc = dquot_initialize(new_ip);
1156 if (rc)
1157 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
1159
1160 /*
1161 * The real work starts here
1162 */
1163 tid = txBegin(new_dir->i_sb, 0);
1164
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001165 /*
1166 * How do we know the locking is safe from deadlocks?
1167 * The vfs does the hard part for us. Any time we are taking nested
1168 * commit_mutexes, the vfs already has i_mutex held on the parent.
1169 * Here, the vfs has already taken i_mutex on both old_dir and new_dir.
1170 */
1171 mutex_lock_nested(&JFS_IP(new_dir)->commit_mutex, COMMIT_MUTEX_PARENT);
1172 mutex_lock_nested(&JFS_IP(old_ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if (old_dir != new_dir)
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001174 mutex_lock_nested(&JFS_IP(old_dir)->commit_mutex,
1175 COMMIT_MUTEX_SECOND_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 if (new_ip) {
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001178 mutex_lock_nested(&JFS_IP(new_ip)->commit_mutex,
1179 COMMIT_MUTEX_VICTIM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 /*
1181 * Change existing directory entry to new inode number
1182 */
1183 ino = new_ip->i_ino;
1184 rc = dtModify(tid, new_dir, &new_dname, &ino,
1185 old_ip->i_ino, JFS_RENAME);
1186 if (rc)
Dave Kleikamp26456952015-07-15 12:52:47 -05001187 goto out_tx;
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001188 drop_nlink(new_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (S_ISDIR(new_ip->i_mode)) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001190 drop_nlink(new_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 if (new_ip->i_nlink) {
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -05001192 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 if (old_dir != new_dir)
Ingo Molnar1de87442006-01-24 15:22:50 -06001194 mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -05001195 mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
1196 mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if (!S_ISDIR(old_ip->i_mode) && new_ip)
1198 IWRITE_UNLOCK(new_ip);
1199 jfs_error(new_ip->i_sb,
Joe Percheseb8630d2013-06-04 16:39:15 -07001200 "new_ip->i_nlink != 0\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 return -EIO;
1202 }
1203 tblk = tid_to_tblock(tid);
1204 tblk->xflag |= COMMIT_DELETE;
1205 tblk->u.ip = new_ip;
1206 } else if (new_ip->i_nlink == 0) {
1207 assert(!test_cflag(COMMIT_Nolink, new_ip));
1208 /* free block resources */
1209 if ((new_size = commitZeroLink(tid, new_ip)) < 0) {
1210 txAbort(tid, 1); /* Marks FS Dirty */
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001211 rc = new_size;
Dave Kleikamp26456952015-07-15 12:52:47 -05001212 goto out_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
1214 tblk = tid_to_tblock(tid);
1215 tblk->xflag |= COMMIT_DELETE;
1216 tblk->u.ip = new_ip;
1217 } else {
Deepa Dinamani078cd822016-09-14 07:48:04 -07001218 new_ip->i_ctime = current_time(new_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 mark_inode_dirty(new_ip);
1220 }
1221 } else {
1222 /*
1223 * Add new directory entry
1224 */
1225 rc = dtSearch(new_dir, &new_dname, &ino, &btstack,
1226 JFS_CREATE);
1227 if (rc) {
Joe Perches6ed71e92016-03-30 05:23:18 -07001228 jfs_err("jfs_rename didn't expect dtSearch to fail w/rc = %d",
1229 rc);
Dave Kleikamp26456952015-07-15 12:52:47 -05001230 goto out_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
1232
1233 ino = old_ip->i_ino;
1234 rc = dtInsert(tid, new_dir, &new_dname, &ino, &btstack);
1235 if (rc) {
1236 if (rc == -EIO)
1237 jfs_err("jfs_rename: dtInsert returned -EIO");
Dave Kleikamp26456952015-07-15 12:52:47 -05001238 goto out_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240 if (S_ISDIR(old_ip->i_mode))
Dave Hansend8c76e62006-09-30 23:29:04 -07001241 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 }
1243 /*
1244 * Remove old directory entry
1245 */
1246
1247 ino = old_ip->i_ino;
1248 rc = dtDelete(tid, old_dir, &old_dname, &ino, JFS_REMOVE);
1249 if (rc) {
1250 jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
1251 rc);
1252 txAbort(tid, 1); /* Marks Filesystem dirty */
Dave Kleikamp26456952015-07-15 12:52:47 -05001253 goto out_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
1255 if (S_ISDIR(old_ip->i_mode)) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001256 drop_nlink(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 if (old_dir != new_dir) {
1258 /*
1259 * Change inode number of parent for moved directory
1260 */
1261
1262 JFS_IP(old_ip)->i_dtroot.header.idotdot =
1263 cpu_to_le32(new_dir->i_ino);
1264
1265 /* Linelock header of dtree */
1266 tlck = txLock(tid, old_ip,
1267 (struct metapage *) &JFS_IP(old_ip)->bxflag,
1268 tlckDTREE | tlckBTROOT | tlckRELINK);
1269 dtlck = (struct dt_lock *) & tlck->lock;
1270 ASSERT(dtlck->index == 0);
1271 lv = & dtlck->lv[0];
1272 lv->offset = 0;
1273 lv->length = 1;
1274 dtlck->index++;
1275 }
1276 }
1277
1278 /*
1279 * Update ctime on changed/moved inodes & mark dirty
1280 */
Deepa Dinamani078cd822016-09-14 07:48:04 -07001281 old_ip->i_ctime = current_time(old_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 mark_inode_dirty(old_ip);
1283
Deepa Dinamanic2050a42016-09-14 07:48:06 -07001284 new_dir->i_ctime = new_dir->i_mtime = current_time(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 mark_inode_dirty(new_dir);
1286
1287 /* Build list of inodes modified by this transaction */
1288 ipcount = 0;
1289 iplist[ipcount++] = old_ip;
1290 if (new_ip)
1291 iplist[ipcount++] = new_ip;
1292 iplist[ipcount++] = old_dir;
1293
1294 if (old_dir != new_dir) {
1295 iplist[ipcount++] = new_dir;
Deepa Dinamani078cd822016-09-14 07:48:04 -07001296 old_dir->i_ctime = old_dir->i_mtime = current_time(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 mark_inode_dirty(old_dir);
1298 }
1299
1300 /*
1301 * Incomplete truncate of file data can
1302 * result in timing problems unless we synchronously commit the
1303 * transaction.
1304 */
1305 if (new_size)
1306 commit_flag = COMMIT_SYNC;
1307 else
1308 commit_flag = 0;
1309
1310 rc = txCommit(tid, ipcount, iplist, commit_flag);
1311
Dave Kleikamp26456952015-07-15 12:52:47 -05001312 out_tx:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 txEnd(tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if (new_ip)
Ingo Molnar1de87442006-01-24 15:22:50 -06001315 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -05001316 if (old_dir != new_dir)
1317 mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
1318 mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
1319 mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 while (new_size && (rc == 0)) {
1322 tid = txBegin(new_ip->i_sb, 0);
Ingo Molnar1de87442006-01-24 15:22:50 -06001323 mutex_lock(&JFS_IP(new_ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 new_size = xtTruncate_pmap(tid, new_ip, new_size);
1325 if (new_size < 0) {
1326 txAbort(tid, 1);
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001327 rc = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 } else
1329 rc = txCommit(tid, 1, &new_ip, COMMIT_SYNC);
1330 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -06001331 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
1333 if (new_ip && (new_ip->i_nlink == 0))
1334 set_cflag(COMMIT_Nolink, new_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 /*
1336 * Truncating the directory index table is not guaranteed. It
1337 * may need to be done iteratively
1338 */
1339 if (test_cflag(COMMIT_Stale, old_dir)) {
1340 if (old_dir->i_size > 1)
1341 jfs_truncate_nolock(old_dir, 0);
1342
1343 clear_cflag(COMMIT_Stale, old_dir);
1344 }
Dave Kleikampacc84b02015-07-15 13:53:19 -05001345 out_unlock:
Dave Kleikamp26456952015-07-15 12:52:47 -05001346 if (new_ip && !S_ISDIR(new_ip->i_mode))
1347 IWRITE_UNLOCK(new_ip);
1348 out3:
1349 free_UCSname(&new_dname);
1350 out2:
1351 free_UCSname(&old_dname);
1352 out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 jfs_info("jfs_rename: returning %d", rc);
1354 return rc;
1355}
1356
1357
1358/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001359 * NAME: jfs_mknod
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001361 * FUNCTION: Create a special file (device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 */
1363static int jfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04001364 umode_t mode, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
1366 struct jfs_inode_info *jfs_ip;
1367 struct btstack btstack;
1368 struct component_name dname;
1369 ino_t ino;
1370 struct inode *ip;
1371 struct inode *iplist[2];
1372 int rc;
1373 tid_t tid;
1374 struct tblock *tblk;
1375
Al Viroa4555892014-10-21 20:11:25 -04001376 jfs_info("jfs_mknod: %pd", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Dave Kleikampacc84b02015-07-15 13:53:19 -05001378 rc = dquot_initialize(dir);
1379 if (rc)
1380 goto out;
Christoph Hellwig907f4552010-03-03 09:05:06 -05001381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 if ((rc = get_UCSname(&dname, dentry)))
1383 goto out;
1384
1385 ip = ialloc(dir, mode);
Akinobu Mita087387f2006-09-14 09:22:38 -05001386 if (IS_ERR(ip)) {
1387 rc = PTR_ERR(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 goto out1;
1389 }
1390 jfs_ip = JFS_IP(ip);
1391
1392 tid = txBegin(dir->i_sb, 0);
1393
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001394 mutex_lock_nested(&JFS_IP(dir)->commit_mutex, COMMIT_MUTEX_PARENT);
1395 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001397 rc = jfs_init_acl(tid, ip, dir);
1398 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 goto out3;
1400
Eric Paris2a7dba32011-02-01 11:05:39 -05001401 rc = jfs_init_security(tid, ip, dir, &dentry->d_name);
Dave Kleikamp1d15b10f2005-09-01 09:05:39 -05001402 if (rc) {
1403 txAbort(tid, 0);
1404 goto out3;
1405 }
1406
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001407 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE))) {
1408 txAbort(tid, 0);
1409 goto out3;
1410 }
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 tblk = tid_to_tblock(tid);
1413 tblk->xflag |= COMMIT_CREATE;
1414 tblk->ino = ip->i_ino;
1415 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
1416
1417 ino = ip->i_ino;
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001418 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack))) {
1419 txAbort(tid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 goto out3;
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423 ip->i_op = &jfs_file_inode_operations;
1424 jfs_ip->dev = new_encode_dev(rdev);
1425 init_special_inode(ip, ip->i_mode, rdev);
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 mark_inode_dirty(ip);
1428
Deepa Dinamani078cd822016-09-14 07:48:04 -07001429 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 mark_inode_dirty(dir);
1432
1433 iplist[0] = dir;
1434 iplist[1] = ip;
1435 rc = txCommit(tid, 2, iplist, 0);
1436
1437 out3:
1438 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -06001439 mutex_unlock(&JFS_IP(ip)->commit_mutex);
1440 mutex_unlock(&JFS_IP(dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (rc) {
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001442 free_ea_wmap(ip);
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02001443 clear_nlink(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001444 unlock_new_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 iput(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001446 } else {
Al Viro2d2d3f12018-05-04 08:23:01 -04001447 d_instantiate_new(dentry, ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 out1:
1451 free_UCSname(&dname);
1452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 out:
1454 jfs_info("jfs_mknod: returning %d", rc);
1455 return rc;
1456}
1457
Al Viro00cd8dd2012-06-10 17:13:09 -04001458static struct dentry *jfs_lookup(struct inode *dip, struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
1460 struct btstack btstack;
1461 ino_t inum;
1462 struct inode *ip;
1463 struct component_name key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 int rc;
1465
Al Viroa4555892014-10-21 20:11:25 -04001466 jfs_info("jfs_lookup: name = %pd", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Al Viro79ac5a42011-07-17 10:17:46 -04001468 if ((rc = get_UCSname(&key, dentry)))
1469 return ERR_PTR(rc);
1470 rc = dtSearch(dip, &key, &inum, &btstack, JFS_LOOKUP);
1471 free_UCSname(&key);
1472 if (rc == -ENOENT) {
1473 ip = NULL;
1474 } else if (rc) {
1475 jfs_err("jfs_lookup: dtSearch returned %d", rc);
1476 ip = ERR_PTR(rc);
1477 } else {
1478 ip = jfs_iget(dip->i_sb, inum);
1479 if (IS_ERR(ip))
1480 jfs_err("jfs_lookup: iget failed on inum %d", (uint)inum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
1482
Al Viro94b77bd2010-12-18 10:59:31 -05001483 return d_splice_alias(ip, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484}
1485
Christoph Hellwigd425de72007-10-21 16:42:09 -07001486static struct inode *jfs_nfs_get_inode(struct super_block *sb,
1487 u64 ino, u32 generation)
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001488{
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001489 struct inode *inode;
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001490
1491 if (ino == 0)
1492 return ERR_PTR(-ESTALE);
David Howellseab1df72008-02-07 00:15:43 -08001493 inode = jfs_iget(sb, ino);
1494 if (IS_ERR(inode))
1495 return ERR_CAST(inode);
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001496
David Howellseab1df72008-02-07 00:15:43 -08001497 if (generation && inode->i_generation != generation) {
Christoph Hellwigd425de72007-10-21 16:42:09 -07001498 iput(inode);
1499 return ERR_PTR(-ESTALE);
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001500 }
1501
Christoph Hellwigd425de72007-10-21 16:42:09 -07001502 return inode;
1503}
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001504
Christoph Hellwigd425de72007-10-21 16:42:09 -07001505struct dentry *jfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
1506 int fh_len, int fh_type)
1507{
1508 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1509 jfs_nfs_get_inode);
1510}
1511
1512struct dentry *jfs_fh_to_parent(struct super_block *sb, struct fid *fid,
1513 int fh_len, int fh_type)
1514{
1515 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1516 jfs_nfs_get_inode);
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001517}
1518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519struct dentry *jfs_get_parent(struct dentry *dentry)
1520{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 unsigned long parent_ino;
1522
1523 parent_ino =
David Howells2b0143b2015-03-17 22:25:59 +00001524 le32_to_cpu(JFS_IP(d_inode(dentry))->i_dtroot.header.idotdot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Al Virofc640052016-04-10 01:33:30 -04001526 return d_obtain_alias(jfs_iget(dentry->d_sb, parent_ino));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527}
1528
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001529const struct inode_operations jfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 .create = jfs_create,
1531 .lookup = jfs_lookup,
1532 .link = jfs_link,
1533 .unlink = jfs_unlink,
1534 .symlink = jfs_symlink,
1535 .mkdir = jfs_mkdir,
1536 .rmdir = jfs_rmdir,
1537 .mknod = jfs_mknod,
1538 .rename = jfs_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 .listxattr = jfs_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 .setattr = jfs_setattr,
Christoph Hellwig759bfee2010-03-03 09:05:02 -05001541#ifdef CONFIG_JFS_POSIX_ACL
Christoph Hellwig4e34e712011-07-23 17:37:31 +02001542 .get_acl = jfs_get_acl,
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -08001543 .set_acl = jfs_set_acl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544#endif
1545};
1546
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001547const struct file_operations jfs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 .read = generic_read_dir,
Al Viro070a0eb2013-05-17 17:00:34 -04001549 .iterate = jfs_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 .fsync = jfs_fsync,
Andi Kleenbaab81f2008-01-27 16:58:51 -06001551 .unlocked_ioctl = jfs_ioctl,
Andi Kleenef1fc2f2008-01-27 17:02:02 -06001552#ifdef CONFIG_COMPAT
1553 .compat_ioctl = jfs_compat_ioctl,
1554#endif
Christoph Hellwig3222a3e2008-09-03 21:53:01 +02001555 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556};
1557
Linus Torvaldsda53be12013-05-21 15:22:44 -07001558static int jfs_ci_hash(const struct dentry *dir, struct qstr *this)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
1560 unsigned long hash;
1561 int i;
1562
Linus Torvalds8387ff22016-06-10 07:51:30 -07001563 hash = init_name_hash(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 for (i=0; i < this->len; i++)
1565 hash = partial_name_hash(tolower(this->name[i]), hash);
1566 this->hash = end_name_hash(hash);
1567
1568 return 0;
1569}
1570
Al Viro6fa67e72016-07-31 16:37:25 -04001571static int jfs_ci_compare(const struct dentry *dentry,
Nick Piggin621e1552011-01-07 17:49:27 +11001572 unsigned int len, const char *str, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573{
1574 int i, result = 1;
1575
Nick Piggin621e1552011-01-07 17:49:27 +11001576 if (len != name->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 goto out;
Nick Piggin621e1552011-01-07 17:49:27 +11001578 for (i=0; i < len; i++) {
1579 if (tolower(str[i]) != tolower(name->name[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 goto out;
1581 }
1582 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583out:
1584 return result;
1585}
1586
Al Viro0b728e12012-06-10 16:03:43 -04001587static int jfs_ci_revalidate(struct dentry *dentry, unsigned int flags)
Nick Piggin2bc334d2011-01-07 17:49:25 +11001588{
1589 /*
1590 * This is not negative dentry. Always valid.
1591 *
1592 * Note, rename() to existing directory entry will have ->d_inode,
1593 * and will use existing name which isn't specified name by user.
1594 *
1595 * We may be able to drop this positive dentry here. But dropping
1596 * positive dentry isn't good idea. So it's unsupported like
1597 * rename("filename", "FILENAME") for now.
1598 */
David Howells2b0143b2015-03-17 22:25:59 +00001599 if (d_really_is_positive(dentry))
Nick Piggin2bc334d2011-01-07 17:49:25 +11001600 return 1;
1601
1602 /*
1603 * This may be nfsd (or something), anyway, we can't see the
1604 * intent of this. So, since this can be for creation, drop it.
1605 */
Al Viro0b728e12012-06-10 16:03:43 -04001606 if (!flags)
Nick Piggin2bc334d2011-01-07 17:49:25 +11001607 return 0;
1608
1609 /*
1610 * Drop the negative dentry, in order to make sure to use the
1611 * case sensitive name which is specified by user if this is
1612 * for creation.
1613 */
Al Viro0b728e12012-06-10 16:03:43 -04001614 if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
Al Viro407938e2011-06-25 21:37:18 -04001615 return 0;
Nick Piggin2bc334d2011-01-07 17:49:25 +11001616 return 1;
1617}
1618
Al Viroad28b4e2009-02-20 06:00:49 +00001619const struct dentry_operations jfs_ci_dentry_operations =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
1621 .d_hash = jfs_ci_hash,
1622 .d_compare = jfs_ci_compare,
Nick Piggin2bc334d2011-01-07 17:49:25 +11001623 .d_revalidate = jfs_ci_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624};