blob: b41596d71858b4a1fcaf4b3e133ebc7b72d48afd [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 {
Dave Kleikamp1f3403f2008-12-30 22:08:37 -0600181 unlock_new_inode(ip);
Al Viro8fc37ec2012-07-19 09:18:15 +0400182 d_instantiate(dentry, ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -0600183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 out2:
186 free_UCSname(&dname);
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 out1:
189
190 jfs_info("jfs_create: rc:%d", rc);
191 return rc;
192}
193
194
195/*
196 * NAME: jfs_mkdir(dip, dentry, mode)
197 *
198 * FUNCTION: create a child directory in the parent directory <dip>
199 * with name = <from dentry> and mode = <mode>
200 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500201 * PARAMETER: dip - parent directory vnode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 * dentry - dentry of child directory
203 * mode - create mode (rwxrwxrwx).
204 *
205 * RETURN: Errors from subroutines
206 *
207 * note:
208 * EACCESS: user needs search+write permission on the parent directory
209 */
Al Viro18bb1db2011-07-26 01:41:39 -0400210static int jfs_mkdir(struct inode *dip, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 int rc = 0;
213 tid_t tid; /* transaction id */
214 struct inode *ip = NULL; /* child directory inode */
215 ino_t ino;
216 struct component_name dname; /* child directory name */
217 struct btstack btstack;
218 struct inode *iplist[2];
219 struct tblock *tblk;
220
Al Viroa4555892014-10-21 20:11:25 -0400221 jfs_info("jfs_mkdir: dip:0x%p name:%pd", dip, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Dave Kleikampacc84b02015-07-15 13:53:19 -0500223 rc = dquot_initialize(dip);
224 if (rc)
225 goto out1;
Christoph Hellwig907f4552010-03-03 09:05:06 -0500226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 /*
228 * search parent directory for entry/freespace
229 * (dtSearch() returns parent directory page pinned)
230 */
231 if ((rc = get_UCSname(&dname, dentry)))
232 goto out1;
233
234 /*
235 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
236 * block there while holding dtree page, so we allocate the inode &
237 * begin the transaction before we search the directory.
238 */
239 ip = ialloc(dip, S_IFDIR | mode);
Akinobu Mita087387f2006-09-14 09:22:38 -0500240 if (IS_ERR(ip)) {
241 rc = PTR_ERR(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 goto out2;
243 }
244
245 tid = txBegin(dip->i_sb, 0);
246
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600247 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
248 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500250 rc = jfs_init_acl(tid, ip, dip);
251 if (rc)
252 goto out3;
253
Eric Paris2a7dba32011-02-01 11:05:39 -0500254 rc = jfs_init_security(tid, ip, dip, &dentry->d_name);
Dave Kleikamp1d15b10f2005-09-01 09:05:39 -0500255 if (rc) {
256 txAbort(tid, 0);
257 goto out3;
258 }
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
261 jfs_err("jfs_mkdir: dtSearch returned %d", rc);
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500262 txAbort(tid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 goto out3;
264 }
265
266 tblk = tid_to_tblock(tid);
267 tblk->xflag |= COMMIT_CREATE;
268 tblk->ino = ip->i_ino;
269 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
270
271 iplist[0] = dip;
272 iplist[1] = ip;
273
274 /*
275 * initialize the child directory in-line in inode
276 */
277 dtInitRoot(tid, ip, dip->i_ino);
278
279 /*
280 * create entry in parent directory for child directory
281 * (dtInsert() releases parent directory page)
282 */
283 ino = ip->i_ino;
284 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
285 if (rc == -EIO) {
286 jfs_err("jfs_mkdir: dtInsert returned -EIO");
287 txAbort(tid, 1); /* Marks Filesystem dirty */
288 } else
289 txAbort(tid, 0); /* Filesystem full */
290 goto out3;
291 }
292
Miklos Szeredibfe86842011-10-28 14:13:29 +0200293 set_nlink(ip, 2); /* for '.' */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 ip->i_op = &jfs_dir_inode_operations;
295 ip->i_fop = &jfs_dir_operations;
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 mark_inode_dirty(ip);
298
299 /* update parent directory inode */
Dave Hansend8c76e62006-09-30 23:29:04 -0700300 inc_nlink(dip); /* for '..' from child directory */
Deepa Dinamani078cd822016-09-14 07:48:04 -0700301 dip->i_ctime = dip->i_mtime = current_time(dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 mark_inode_dirty(dip);
303
304 rc = txCommit(tid, 2, &iplist[0], 0);
305
306 out3:
307 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600308 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500309 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (rc) {
Dave Kleikamp4f4b4012005-09-01 09:02:43 -0500311 free_ea_wmap(ip);
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200312 clear_nlink(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -0600313 unlock_new_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 iput(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -0600315 } else {
Dave Kleikamp1f3403f2008-12-30 22:08:37 -0600316 unlock_new_inode(ip);
Al Viro8fc37ec2012-07-19 09:18:15 +0400317 d_instantiate(dentry, ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -0600318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 out2:
321 free_UCSname(&dname);
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 out1:
325
326 jfs_info("jfs_mkdir: rc:%d", rc);
327 return rc;
328}
329
330/*
331 * NAME: jfs_rmdir(dip, dentry)
332 *
333 * FUNCTION: remove a link to child directory
334 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500335 * PARAMETER: dip - parent inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 * dentry - child directory dentry
337 *
338 * RETURN: -EINVAL - if name is . or ..
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500339 * -EINVAL - if . or .. exist but are invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 * errors from subroutines
341 *
342 * note:
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500343 * if other threads have the directory open when the last link
344 * is removed, the "." and ".." entries, if present, are removed before
345 * rmdir() returns and no new entries may be created in the directory,
346 * but the directory is not removed until the last reference to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 * the directory is released (cf.unlink() of regular file).
348 */
349static int jfs_rmdir(struct inode *dip, struct dentry *dentry)
350{
351 int rc;
352 tid_t tid; /* transaction id */
David Howells2b0143b2015-03-17 22:25:59 +0000353 struct inode *ip = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 ino_t ino;
355 struct component_name dname;
356 struct inode *iplist[2];
357 struct tblock *tblk;
358
Al Viroa4555892014-10-21 20:11:25 -0400359 jfs_info("jfs_rmdir: dip:0x%p name:%pd", dip, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 /* Init inode for quota operations. */
Dave Kleikampacc84b02015-07-15 13:53:19 -0500362 rc = dquot_initialize(dip);
363 if (rc)
364 goto out;
365 rc = dquot_initialize(ip);
366 if (rc)
367 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 /* directory must be empty to be removed */
370 if (!dtEmpty(ip)) {
371 rc = -ENOTEMPTY;
372 goto out;
373 }
374
375 if ((rc = get_UCSname(&dname, dentry))) {
376 goto out;
377 }
378
379 tid = txBegin(dip->i_sb, 0);
380
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600381 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
382 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 iplist[0] = dip;
385 iplist[1] = ip;
386
387 tblk = tid_to_tblock(tid);
388 tblk->xflag |= COMMIT_DELETE;
389 tblk->u.ip = ip;
390
391 /*
392 * delete the entry of target directory from parent directory
393 */
394 ino = ip->i_ino;
395 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
396 jfs_err("jfs_rmdir: dtDelete returned %d", rc);
397 if (rc == -EIO)
398 txAbort(tid, 1);
399 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600400 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500401 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 goto out2;
404 }
405
406 /* update parent directory's link count corresponding
407 * to ".." entry of the target directory deleted
408 */
Deepa Dinamani078cd822016-09-14 07:48:04 -0700409 dip->i_ctime = dip->i_mtime = current_time(dip);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700410 inode_dec_link_count(dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 /*
413 * OS/2 could have created EA and/or ACL
414 */
415 /* free EA from both persistent and working map */
416 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
417 /* free EA pages */
418 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
419 }
420 JFS_IP(ip)->ea.flag = 0;
421
422 /* free ACL from both persistent and working map */
423 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
424 /* free ACL pages */
425 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
426 }
427 JFS_IP(ip)->acl.flag = 0;
428
429 /* mark the target directory as deleted */
Dave Hansence71ec32006-09-30 23:29:06 -0700430 clear_nlink(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 mark_inode_dirty(ip);
432
433 rc = txCommit(tid, 2, &iplist[0], 0);
434
435 txEnd(tid);
436
Ingo Molnar1de87442006-01-24 15:22:50 -0600437 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500438 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 /*
441 * Truncating the directory index table is not guaranteed. It
442 * may need to be done iteratively
443 */
444 if (test_cflag(COMMIT_Stale, dip)) {
445 if (dip->i_size > 1)
446 jfs_truncate_nolock(dip, 0);
447
448 clear_cflag(COMMIT_Stale, dip);
449 }
450
451 out2:
452 free_UCSname(&dname);
453
454 out:
455 jfs_info("jfs_rmdir: rc:%d", rc);
456 return rc;
457}
458
459/*
460 * NAME: jfs_unlink(dip, dentry)
461 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500462 * FUNCTION: remove a link to object <vp> named by <name>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 * from parent directory <dvp>
464 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500465 * PARAMETER: dip - inode of parent directory
466 * dentry - dentry of object to be removed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 *
468 * RETURN: errors from subroutines
469 *
470 * note:
471 * temporary file: if one or more processes have the file open
472 * when the last link is removed, the link will be removed before
473 * unlink() returns, but the removal of the file contents will be
474 * postponed until all references to the files are closed.
475 *
476 * JFS does NOT support unlink() on directories.
477 *
478 */
479static int jfs_unlink(struct inode *dip, struct dentry *dentry)
480{
481 int rc;
482 tid_t tid; /* transaction id */
David Howells2b0143b2015-03-17 22:25:59 +0000483 struct inode *ip = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 ino_t ino;
485 struct component_name dname; /* object name */
486 struct inode *iplist[2];
487 struct tblock *tblk;
488 s64 new_size = 0;
489 int commit_flag;
490
Al Viroa4555892014-10-21 20:11:25 -0400491 jfs_info("jfs_unlink: dip:0x%p name:%pd", dip, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 /* Init inode for quota operations. */
Dave Kleikampacc84b02015-07-15 13:53:19 -0500494 rc = dquot_initialize(dip);
495 if (rc)
496 goto out;
497 rc = dquot_initialize(ip);
498 if (rc)
499 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 if ((rc = get_UCSname(&dname, dentry)))
502 goto out;
503
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600504 IWRITE_LOCK(ip, RDWRLOCK_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 tid = txBegin(dip->i_sb, 0);
507
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600508 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
509 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 iplist[0] = dip;
512 iplist[1] = ip;
513
514 /*
515 * delete the entry of target file from parent directory
516 */
517 ino = ip->i_ino;
518 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
519 jfs_err("jfs_unlink: dtDelete returned %d", rc);
520 if (rc == -EIO)
521 txAbort(tid, 1); /* Marks FS Dirty */
522 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600523 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500524 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 IWRITE_UNLOCK(ip);
526 goto out1;
527 }
528
529 ASSERT(ip->i_nlink);
530
Deepa Dinamani078cd822016-09-14 07:48:04 -0700531 ip->i_ctime = dip->i_ctime = dip->i_mtime = current_time(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 mark_inode_dirty(dip);
533
534 /* update target's inode */
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700535 inode_dec_link_count(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 /*
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500538 * commit zero link count object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 */
540 if (ip->i_nlink == 0) {
541 assert(!test_cflag(COMMIT_Nolink, ip));
542 /* free block resources */
543 if ((new_size = commitZeroLink(tid, ip)) < 0) {
544 txAbort(tid, 1); /* Marks FS Dirty */
545 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600546 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500547 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 IWRITE_UNLOCK(ip);
549 rc = new_size;
550 goto out1;
551 }
552 tblk = tid_to_tblock(tid);
553 tblk->xflag |= COMMIT_DELETE;
554 tblk->u.ip = ip;
555 }
556
557 /*
558 * Incomplete truncate of file data can
559 * result in timing problems unless we synchronously commit the
560 * transaction.
561 */
562 if (new_size)
563 commit_flag = COMMIT_SYNC;
564 else
565 commit_flag = 0;
566
567 /*
568 * If xtTruncate was incomplete, commit synchronously to avoid
569 * timing complications
570 */
571 rc = txCommit(tid, 2, &iplist[0], commit_flag);
572
573 txEnd(tid);
574
Ingo Molnar1de87442006-01-24 15:22:50 -0600575 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500576 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 while (new_size && (rc == 0)) {
579 tid = txBegin(dip->i_sb, 0);
Ingo Molnar1de87442006-01-24 15:22:50 -0600580 mutex_lock(&JFS_IP(ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 new_size = xtTruncate_pmap(tid, ip, new_size);
582 if (new_size < 0) {
583 txAbort(tid, 1); /* Marks FS Dirty */
584 rc = new_size;
585 } else
586 rc = txCommit(tid, 2, &iplist[0], COMMIT_SYNC);
587 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -0600588 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
590
591 if (ip->i_nlink == 0)
592 set_cflag(COMMIT_Nolink, ip);
593
594 IWRITE_UNLOCK(ip);
595
596 /*
597 * Truncating the directory index table is not guaranteed. It
598 * may need to be done iteratively
599 */
600 if (test_cflag(COMMIT_Stale, dip)) {
601 if (dip->i_size > 1)
602 jfs_truncate_nolock(dip, 0);
603
604 clear_cflag(COMMIT_Stale, dip);
605 }
606
607 out1:
608 free_UCSname(&dname);
609 out:
610 jfs_info("jfs_unlink: rc:%d", rc);
611 return rc;
612}
613
614/*
615 * NAME: commitZeroLink()
616 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500617 * FUNCTION: for non-directory, called by jfs_remove(),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 * truncate a regular file, directory or symbolic
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500619 * link to zero length. return 0 if type is not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 * one of these.
621 *
622 * if the file is currently associated with a VM segment
623 * only permanent disk and inode map resources are freed,
624 * and neither the inode nor indirect blocks are modified
625 * so that the resources can be later freed in the work
626 * map by ctrunc1.
627 * if there is no VM segment on entry, the resources are
628 * freed in both work and permanent map.
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500629 * (? for temporary file - memory object is cached even
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 * after no reference:
631 * reference count > 0 - )
632 *
633 * PARAMETERS: cd - pointer to commit data structure.
634 * current inode is the one to truncate.
635 *
636 * RETURN: Errors from subroutines
637 */
638static s64 commitZeroLink(tid_t tid, struct inode *ip)
639{
640 int filetype;
641 struct tblock *tblk;
642
643 jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid, ip);
644
645 filetype = ip->i_mode & S_IFMT;
646 switch (filetype) {
647 case S_IFREG:
648 break;
649 case S_IFLNK:
650 /* fast symbolic link */
651 if (ip->i_size < IDATASIZE) {
652 ip->i_size = 0;
653 return 0;
654 }
655 break;
656 default:
657 assert(filetype != S_IFDIR);
658 return 0;
659 }
660
661 set_cflag(COMMIT_Freewmap, ip);
662
663 /* mark transaction of block map update type */
664 tblk = tid_to_tblock(tid);
665 tblk->xflag |= COMMIT_PMAP;
666
667 /*
668 * free EA
669 */
670 if (JFS_IP(ip)->ea.flag & DXD_EXTENT)
671 /* acquire maplock on EA to be freed from block map */
672 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
673
674 /*
675 * free ACL
676 */
677 if (JFS_IP(ip)->acl.flag & DXD_EXTENT)
678 /* acquire maplock on EA to be freed from block map */
679 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
680
681 /*
682 * free xtree/data (truncate to zero length):
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500683 * free xtree/data pages from cache if COMMIT_PWMAP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 * free xtree/data blocks from persistent block map, and
685 * free xtree/data blocks from working block map if COMMIT_PWMAP;
686 */
687 if (ip->i_size)
688 return xtTruncate_pmap(tid, ip, 0);
689
690 return 0;
691}
692
693
694/*
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500695 * NAME: jfs_free_zero_link()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500697 * FUNCTION: for non-directory, called by iClose(),
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500698 * free resources of a file from cache and WORKING map
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 * for a file previously committed with zero link count
700 * while associated with a pager object,
701 *
702 * PARAMETER: ip - pointer to inode of file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 */
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500704void jfs_free_zero_link(struct inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 int type;
707
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500708 jfs_info("jfs_free_zero_link: ip = 0x%p", ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 /* return if not reg or symbolic link or if size is
711 * already ok.
712 */
713 type = ip->i_mode & S_IFMT;
714
715 switch (type) {
716 case S_IFREG:
717 break;
718 case S_IFLNK:
719 /* if its contained in inode nothing to do */
720 if (ip->i_size < IDATASIZE)
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500721 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 break;
723 default:
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500724 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726
727 /*
728 * free EA
729 */
730 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
731 s64 xaddr = addressDXD(&JFS_IP(ip)->ea);
732 int xlen = lengthDXD(&JFS_IP(ip)->ea);
733 struct maplock maplock; /* maplock for COMMIT_WMAP */
734 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
735
736 /* free EA pages from cache */
737 invalidate_dxd_metapages(ip, JFS_IP(ip)->ea);
738
739 /* free EA extent from working block map */
740 maplock.index = 1;
741 pxdlock = (struct pxd_lock *) & maplock;
742 pxdlock->flag = mlckFREEPXD;
743 PXDaddress(&pxdlock->pxd, xaddr);
744 PXDlength(&pxdlock->pxd, xlen);
745 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
746 }
747
748 /*
749 * free ACL
750 */
751 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
752 s64 xaddr = addressDXD(&JFS_IP(ip)->acl);
753 int xlen = lengthDXD(&JFS_IP(ip)->acl);
754 struct maplock maplock; /* maplock for COMMIT_WMAP */
755 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
756
757 invalidate_dxd_metapages(ip, JFS_IP(ip)->acl);
758
759 /* free ACL extent from working block map */
760 maplock.index = 1;
761 pxdlock = (struct pxd_lock *) & maplock;
762 pxdlock->flag = mlckFREEPXD;
763 PXDaddress(&pxdlock->pxd, xaddr);
764 PXDlength(&pxdlock->pxd, xlen);
765 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
766 }
767
768 /*
769 * free xtree/data (truncate to zero length):
770 * free xtree/data pages from cache, and
771 * free xtree/data blocks from working block map;
772 */
773 if (ip->i_size)
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500774 xtTruncate(0, ip, 0, COMMIT_WMAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
777/*
778 * NAME: jfs_link(vp, dvp, name, crp)
779 *
780 * FUNCTION: create a link to <vp> by the name = <name>
781 * in the parent directory <dvp>
782 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500783 * PARAMETER: vp - target object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 * dvp - parent directory of new link
785 * name - name of new link to target object
786 * crp - credential
787 *
788 * RETURN: Errors from subroutines
789 *
790 * note:
791 * JFS does NOT support link() on directories (to prevent circular
792 * path in the directory hierarchy);
793 * EPERM: the target object is a directory, and either the caller
794 * does not have appropriate privileges or the implementation prohibits
795 * using link() on directories [XPG4.2].
796 *
797 * JFS does NOT support links between file systems:
798 * EXDEV: target object and new link are on different file systems and
799 * implementation does not support links between file systems [XPG4.2].
800 */
801static int jfs_link(struct dentry *old_dentry,
802 struct inode *dir, struct dentry *dentry)
803{
804 int rc;
805 tid_t tid;
David Howells2b0143b2015-03-17 22:25:59 +0000806 struct inode *ip = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 ino_t ino;
808 struct component_name dname;
809 struct btstack btstack;
810 struct inode *iplist[2];
811
Al Viroa4555892014-10-21 20:11:25 -0400812 jfs_info("jfs_link: %pd %pd", old_dentry, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Dave Kleikampacc84b02015-07-15 13:53:19 -0500814 rc = dquot_initialize(dir);
815 if (rc)
816 goto out;
Christoph Hellwig907f4552010-03-03 09:05:06 -0500817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 tid = txBegin(ip->i_sb, 0);
819
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600820 mutex_lock_nested(&JFS_IP(dir)->commit_mutex, COMMIT_MUTEX_PARENT);
821 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 /*
824 * scan parent directory for entry/freespace
825 */
826 if ((rc = get_UCSname(&dname, dentry)))
Dave Kleikampacc84b02015-07-15 13:53:19 -0500827 goto out_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
830 goto free_dname;
831
832 /*
833 * create entry for new link in parent directory
834 */
835 ino = ip->i_ino;
836 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
837 goto free_dname;
838
839 /* update object inode */
Dave Hansend8c76e62006-09-30 23:29:04 -0700840 inc_nlink(ip); /* for new link */
Deepa Dinamani078cd822016-09-14 07:48:04 -0700841 ip->i_ctime = current_time(ip);
842 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 mark_inode_dirty(dir);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400844 ihold(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 iplist[0] = ip;
847 iplist[1] = dir;
848 rc = txCommit(tid, 2, &iplist[0], 0);
849
850 if (rc) {
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200851 drop_nlink(ip); /* never instantiated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 iput(ip);
853 } else
854 d_instantiate(dentry, ip);
855
856 free_dname:
857 free_UCSname(&dname);
858
Dave Kleikampacc84b02015-07-15 13:53:19 -0500859 out_tx:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 txEnd(tid);
861
Ingo Molnar1de87442006-01-24 15:22:50 -0600862 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -0500863 mutex_unlock(&JFS_IP(dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Dave Kleikampacc84b02015-07-15 13:53:19 -0500865 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 jfs_info("jfs_link: rc:%d", rc);
867 return rc;
868}
869
870/*
871 * NAME: jfs_symlink(dip, dentry, name)
872 *
873 * FUNCTION: creates a symbolic link to <symlink> by name <name>
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500874 * in directory <dip>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500876 * PARAMETER: dip - parent directory vnode
877 * dentry - dentry of symbolic link
878 * name - the path name of the existing object
879 * that will be the source of the link
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 *
881 * RETURN: errors from subroutines
882 *
883 * note:
884 * ENAMETOOLONG: pathname resolution of a symbolic link produced
885 * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
886*/
887
888static int jfs_symlink(struct inode *dip, struct dentry *dentry,
889 const char *name)
890{
891 int rc;
892 tid_t tid;
893 ino_t ino = 0;
894 struct component_name dname;
895 int ssize; /* source pathname size */
896 struct btstack btstack;
David Howells2b0143b2015-03-17 22:25:59 +0000897 struct inode *ip = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 s64 xlen = 0;
899 int bmask = 0, xsize;
Dave Kleikamp3c2c2262011-06-20 13:00:27 -0500900 s64 xaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 struct metapage *mp;
902 struct super_block *sb;
903 struct tblock *tblk;
904
905 struct inode *iplist[2];
906
907 jfs_info("jfs_symlink: dip:0x%p name:%s", dip, name);
908
Dave Kleikampacc84b02015-07-15 13:53:19 -0500909 rc = dquot_initialize(dip);
910 if (rc)
911 goto out1;
Christoph Hellwig907f4552010-03-03 09:05:06 -0500912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 ssize = strlen(name) + 1;
914
915 /*
916 * search parent directory for entry/freespace
917 * (dtSearch() returns parent directory page pinned)
918 */
919
920 if ((rc = get_UCSname(&dname, dentry)))
921 goto out1;
922
923 /*
924 * allocate on-disk/in-memory inode for symbolic link:
925 * (iAlloc() returns new, locked inode)
926 */
927 ip = ialloc(dip, S_IFLNK | 0777);
Akinobu Mita087387f2006-09-14 09:22:38 -0500928 if (IS_ERR(ip)) {
929 rc = PTR_ERR(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 goto out2;
931 }
932
933 tid = txBegin(dip->i_sb, 0);
934
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600935 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
936 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Eric Paris2a7dba32011-02-01 11:05:39 -0500938 rc = jfs_init_security(tid, ip, dip, &dentry->d_name);
Dave Kleikamp1d15b10f2005-09-01 09:05:39 -0500939 if (rc)
940 goto out3;
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 tblk = tid_to_tblock(tid);
943 tblk->xflag |= COMMIT_CREATE;
944 tblk->ino = ip->i_ino;
945 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
946
947 /* fix symlink access permission
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500948 * (dir_create() ANDs in the u.u_cmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 * but symlinks really need to be 777 access)
950 */
951 ip->i_mode |= 0777;
952
953 /*
954 * write symbolic link target path name
955 */
956 xtInitRoot(tid, ip);
957
958 /*
959 * write source path name inline in on-disk inode (fast symbolic link)
960 */
961
962 if (ssize <= IDATASIZE) {
Dmitry Monakhovc7f2e1f2010-04-16 08:05:50 -0500963 ip->i_op = &jfs_fast_symlink_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Al Viroad476fe2015-05-02 10:41:20 -0400965 ip->i_link = JFS_IP(ip)->i_inline;
966 memcpy(ip->i_link, name, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 ip->i_size = ssize - 1;
968
969 /*
970 * if symlink is > 128 bytes, we don't have the space to
971 * store inline extended attributes
972 */
973 if (ssize > sizeof (JFS_IP(ip)->i_inline))
974 JFS_IP(ip)->mode2 &= ~INLINEEA;
975
976 jfs_info("jfs_symlink: fast symlink added ssize:%d name:%s ",
977 ssize, name);
978 }
979 /*
980 * write source path name in a single extent
981 */
982 else {
983 jfs_info("jfs_symlink: allocate extent ip:0x%p", ip);
984
Dmitry Monakhovc7f2e1f2010-04-16 08:05:50 -0500985 ip->i_op = &jfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -0500986 inode_nohighmem(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 ip->i_mapping->a_ops = &jfs_aops;
988
989 /*
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500990 * even though the data of symlink object (source
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 * path name) is treated as non-journaled user data,
992 * it is read/written thru buffer cache for performance.
993 */
994 sb = ip->i_sb;
995 bmask = JFS_SBI(sb)->bsize - 1;
996 xsize = (ssize + bmask) & ~bmask;
997 xaddr = 0;
998 xlen = xsize >> JFS_SBI(sb)->l2bsize;
999 if ((rc = xtInsert(tid, ip, 0, 0, xlen, &xaddr, 0))) {
1000 txAbort(tid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 goto out3;
1002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 ip->i_size = ssize - 1;
1004 while (ssize) {
1005 /* This is kind of silly since PATH_MAX == 4K */
1006 int copy_size = min(ssize, PSIZE);
1007
1008 mp = get_metapage(ip, xaddr, PSIZE, 1);
1009
1010 if (mp == NULL) {
1011 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
1012 rc = -EIO;
1013 txAbort(tid, 0);
1014 goto out3;
1015 }
1016 memcpy(mp->data, name, copy_size);
1017 flush_metapage(mp);
1018 ssize -= copy_size;
1019 name += copy_size;
1020 xaddr += JFS_SBI(sb)->nbperpage;
1021 }
1022 }
1023
1024 /*
1025 * create entry for symbolic link in parent directory
1026 */
1027 rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE);
1028 if (rc == 0) {
1029 ino = ip->i_ino;
1030 rc = dtInsert(tid, dip, &dname, &ino, &btstack);
1031 }
1032 if (rc) {
1033 if (xlen)
1034 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
1035 txAbort(tid, 0);
1036 /* discard new inode */
1037 goto out3;
1038 }
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 mark_inode_dirty(ip);
1041
Deepa Dinamani078cd822016-09-14 07:48:04 -07001042 dip->i_ctime = dip->i_mtime = current_time(dip);
Dave Kleikamp988a6492005-10-31 16:53:04 -06001043 mark_inode_dirty(dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 /*
1045 * commit update of parent directory and link object
1046 */
1047
1048 iplist[0] = dip;
1049 iplist[1] = ip;
1050 rc = txCommit(tid, 2, &iplist[0], 0);
1051
1052 out3:
1053 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -06001054 mutex_unlock(&JFS_IP(ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -05001055 mutex_unlock(&JFS_IP(dip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (rc) {
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001057 free_ea_wmap(ip);
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02001058 clear_nlink(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001059 unlock_new_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 iput(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001061 } else {
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001062 unlock_new_inode(ip);
Al Viro8fc37ec2012-07-19 09:18:15 +04001063 d_instantiate(dentry, ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 out2:
1067 free_UCSname(&dname);
1068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 out1:
1070 jfs_info("jfs_symlink: rc:%d", rc);
1071 return rc;
1072}
1073
1074
1075/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001076 * NAME: jfs_rename
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001078 * FUNCTION: rename a file or directory
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 */
1080static int jfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredif03b8ad2016-09-27 11:03:57 +02001081 struct inode *new_dir, struct dentry *new_dentry,
1082 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
1084 struct btstack btstack;
1085 ino_t ino;
1086 struct component_name new_dname;
1087 struct inode *new_ip;
1088 struct component_name old_dname;
1089 struct inode *old_ip;
1090 int rc;
1091 tid_t tid;
1092 struct tlock *tlck;
1093 struct dt_lock *dtlck;
1094 struct lv *lv;
1095 int ipcount;
1096 struct inode *iplist[4];
1097 struct tblock *tblk;
1098 s64 new_size = 0;
1099 int commit_flag;
1100
Miklos Szeredif03b8ad2016-09-27 11:03:57 +02001101 if (flags & ~RENAME_NOREPLACE)
1102 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Al Viroa4555892014-10-21 20:11:25 -04001104 jfs_info("jfs_rename: %pd %pd", old_dentry, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Dave Kleikampacc84b02015-07-15 13:53:19 -05001106 rc = dquot_initialize(old_dir);
1107 if (rc)
1108 goto out1;
1109 rc = dquot_initialize(new_dir);
1110 if (rc)
1111 goto out1;
Christoph Hellwig907f4552010-03-03 09:05:06 -05001112
David Howells2b0143b2015-03-17 22:25:59 +00001113 old_ip = d_inode(old_dentry);
1114 new_ip = d_inode(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 if ((rc = get_UCSname(&old_dname, old_dentry)))
1117 goto out1;
1118
1119 if ((rc = get_UCSname(&new_dname, new_dentry)))
1120 goto out2;
1121
1122 /*
1123 * Make sure source inode number is what we think it is
1124 */
1125 rc = dtSearch(old_dir, &old_dname, &ino, &btstack, JFS_LOOKUP);
1126 if (rc || (ino != old_ip->i_ino)) {
1127 rc = -ENOENT;
1128 goto out3;
1129 }
1130
1131 /*
1132 * Make sure dest inode number (if any) is what we think it is
1133 */
1134 rc = dtSearch(new_dir, &new_dname, &ino, &btstack, JFS_LOOKUP);
Joe Perches09aaa742007-11-13 22:16:08 -06001135 if (!rc) {
Dave Kleikampda8a41d2007-11-13 22:25:41 -06001136 if ((!new_ip) || (ino != new_ip->i_ino)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 rc = -ESTALE;
1138 goto out3;
1139 }
1140 } else if (rc != -ENOENT)
1141 goto out3;
1142 else if (new_ip) {
1143 /* no entry exists, but one was expected */
1144 rc = -ESTALE;
1145 goto out3;
1146 }
1147
1148 if (S_ISDIR(old_ip->i_mode)) {
1149 if (new_ip) {
1150 if (!dtEmpty(new_ip)) {
1151 rc = -ENOTEMPTY;
1152 goto out3;
1153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
1155 } else if (new_ip) {
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001156 IWRITE_LOCK(new_ip, RDWRLOCK_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 /* Init inode for quota operations. */
Dave Kleikampacc84b02015-07-15 13:53:19 -05001158 rc = dquot_initialize(new_ip);
1159 if (rc)
1160 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
1162
1163 /*
1164 * The real work starts here
1165 */
1166 tid = txBegin(new_dir->i_sb, 0);
1167
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001168 /*
1169 * How do we know the locking is safe from deadlocks?
1170 * The vfs does the hard part for us. Any time we are taking nested
1171 * commit_mutexes, the vfs already has i_mutex held on the parent.
1172 * Here, the vfs has already taken i_mutex on both old_dir and new_dir.
1173 */
1174 mutex_lock_nested(&JFS_IP(new_dir)->commit_mutex, COMMIT_MUTEX_PARENT);
1175 mutex_lock_nested(&JFS_IP(old_ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (old_dir != new_dir)
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001177 mutex_lock_nested(&JFS_IP(old_dir)->commit_mutex,
1178 COMMIT_MUTEX_SECOND_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180 if (new_ip) {
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001181 mutex_lock_nested(&JFS_IP(new_ip)->commit_mutex,
1182 COMMIT_MUTEX_VICTIM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 /*
1184 * Change existing directory entry to new inode number
1185 */
1186 ino = new_ip->i_ino;
1187 rc = dtModify(tid, new_dir, &new_dname, &ino,
1188 old_ip->i_ino, JFS_RENAME);
1189 if (rc)
Dave Kleikamp26456952015-07-15 12:52:47 -05001190 goto out_tx;
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001191 drop_nlink(new_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 if (S_ISDIR(new_ip->i_mode)) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001193 drop_nlink(new_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if (new_ip->i_nlink) {
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -05001195 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 if (old_dir != new_dir)
Ingo Molnar1de87442006-01-24 15:22:50 -06001197 mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -05001198 mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
1199 mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 if (!S_ISDIR(old_ip->i_mode) && new_ip)
1201 IWRITE_UNLOCK(new_ip);
1202 jfs_error(new_ip->i_sb,
Joe Percheseb8630d2013-06-04 16:39:15 -07001203 "new_ip->i_nlink != 0\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 return -EIO;
1205 }
1206 tblk = tid_to_tblock(tid);
1207 tblk->xflag |= COMMIT_DELETE;
1208 tblk->u.ip = new_ip;
1209 } else if (new_ip->i_nlink == 0) {
1210 assert(!test_cflag(COMMIT_Nolink, new_ip));
1211 /* free block resources */
1212 if ((new_size = commitZeroLink(tid, new_ip)) < 0) {
1213 txAbort(tid, 1); /* Marks FS Dirty */
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001214 rc = new_size;
Dave Kleikamp26456952015-07-15 12:52:47 -05001215 goto out_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
1217 tblk = tid_to_tblock(tid);
1218 tblk->xflag |= COMMIT_DELETE;
1219 tblk->u.ip = new_ip;
1220 } else {
Deepa Dinamani078cd822016-09-14 07:48:04 -07001221 new_ip->i_ctime = current_time(new_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 mark_inode_dirty(new_ip);
1223 }
1224 } else {
1225 /*
1226 * Add new directory entry
1227 */
1228 rc = dtSearch(new_dir, &new_dname, &ino, &btstack,
1229 JFS_CREATE);
1230 if (rc) {
Joe Perches6ed71e92016-03-30 05:23:18 -07001231 jfs_err("jfs_rename didn't expect dtSearch to fail w/rc = %d",
1232 rc);
Dave Kleikamp26456952015-07-15 12:52:47 -05001233 goto out_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
1235
1236 ino = old_ip->i_ino;
1237 rc = dtInsert(tid, new_dir, &new_dname, &ino, &btstack);
1238 if (rc) {
1239 if (rc == -EIO)
1240 jfs_err("jfs_rename: dtInsert returned -EIO");
Dave Kleikamp26456952015-07-15 12:52:47 -05001241 goto out_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 }
1243 if (S_ISDIR(old_ip->i_mode))
Dave Hansend8c76e62006-09-30 23:29:04 -07001244 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
1246 /*
1247 * Remove old directory entry
1248 */
1249
1250 ino = old_ip->i_ino;
1251 rc = dtDelete(tid, old_dir, &old_dname, &ino, JFS_REMOVE);
1252 if (rc) {
1253 jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
1254 rc);
1255 txAbort(tid, 1); /* Marks Filesystem dirty */
Dave Kleikamp26456952015-07-15 12:52:47 -05001256 goto out_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
1258 if (S_ISDIR(old_ip->i_mode)) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001259 drop_nlink(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (old_dir != new_dir) {
1261 /*
1262 * Change inode number of parent for moved directory
1263 */
1264
1265 JFS_IP(old_ip)->i_dtroot.header.idotdot =
1266 cpu_to_le32(new_dir->i_ino);
1267
1268 /* Linelock header of dtree */
1269 tlck = txLock(tid, old_ip,
1270 (struct metapage *) &JFS_IP(old_ip)->bxflag,
1271 tlckDTREE | tlckBTROOT | tlckRELINK);
1272 dtlck = (struct dt_lock *) & tlck->lock;
1273 ASSERT(dtlck->index == 0);
1274 lv = & dtlck->lv[0];
1275 lv->offset = 0;
1276 lv->length = 1;
1277 dtlck->index++;
1278 }
1279 }
1280
1281 /*
1282 * Update ctime on changed/moved inodes & mark dirty
1283 */
Deepa Dinamani078cd822016-09-14 07:48:04 -07001284 old_ip->i_ctime = current_time(old_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 mark_inode_dirty(old_ip);
1286
Deepa Dinamanic2050a42016-09-14 07:48:06 -07001287 new_dir->i_ctime = new_dir->i_mtime = current_time(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 mark_inode_dirty(new_dir);
1289
1290 /* Build list of inodes modified by this transaction */
1291 ipcount = 0;
1292 iplist[ipcount++] = old_ip;
1293 if (new_ip)
1294 iplist[ipcount++] = new_ip;
1295 iplist[ipcount++] = old_dir;
1296
1297 if (old_dir != new_dir) {
1298 iplist[ipcount++] = new_dir;
Deepa Dinamani078cd822016-09-14 07:48:04 -07001299 old_dir->i_ctime = old_dir->i_mtime = current_time(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 mark_inode_dirty(old_dir);
1301 }
1302
1303 /*
1304 * Incomplete truncate of file data can
1305 * result in timing problems unless we synchronously commit the
1306 * transaction.
1307 */
1308 if (new_size)
1309 commit_flag = COMMIT_SYNC;
1310 else
1311 commit_flag = 0;
1312
1313 rc = txCommit(tid, ipcount, iplist, commit_flag);
1314
Dave Kleikamp26456952015-07-15 12:52:47 -05001315 out_tx:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 txEnd(tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 if (new_ip)
Ingo Molnar1de87442006-01-24 15:22:50 -06001318 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
Evgeniy Dushistov48ce8b02006-06-05 08:21:03 -05001319 if (old_dir != new_dir)
1320 mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
1321 mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
1322 mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 while (new_size && (rc == 0)) {
1325 tid = txBegin(new_ip->i_sb, 0);
Ingo Molnar1de87442006-01-24 15:22:50 -06001326 mutex_lock(&JFS_IP(new_ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 new_size = xtTruncate_pmap(tid, new_ip, new_size);
1328 if (new_size < 0) {
1329 txAbort(tid, 1);
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001330 rc = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 } else
1332 rc = txCommit(tid, 1, &new_ip, COMMIT_SYNC);
1333 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -06001334 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 }
1336 if (new_ip && (new_ip->i_nlink == 0))
1337 set_cflag(COMMIT_Nolink, new_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 /*
1339 * Truncating the directory index table is not guaranteed. It
1340 * may need to be done iteratively
1341 */
1342 if (test_cflag(COMMIT_Stale, old_dir)) {
1343 if (old_dir->i_size > 1)
1344 jfs_truncate_nolock(old_dir, 0);
1345
1346 clear_cflag(COMMIT_Stale, old_dir);
1347 }
Dave Kleikampacc84b02015-07-15 13:53:19 -05001348 out_unlock:
Dave Kleikamp26456952015-07-15 12:52:47 -05001349 if (new_ip && !S_ISDIR(new_ip->i_mode))
1350 IWRITE_UNLOCK(new_ip);
1351 out3:
1352 free_UCSname(&new_dname);
1353 out2:
1354 free_UCSname(&old_dname);
1355 out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 jfs_info("jfs_rename: returning %d", rc);
1357 return rc;
1358}
1359
1360
1361/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001362 * NAME: jfs_mknod
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001364 * FUNCTION: Create a special file (device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 */
1366static int jfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04001367 umode_t mode, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
1369 struct jfs_inode_info *jfs_ip;
1370 struct btstack btstack;
1371 struct component_name dname;
1372 ino_t ino;
1373 struct inode *ip;
1374 struct inode *iplist[2];
1375 int rc;
1376 tid_t tid;
1377 struct tblock *tblk;
1378
Al Viroa4555892014-10-21 20:11:25 -04001379 jfs_info("jfs_mknod: %pd", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Dave Kleikampacc84b02015-07-15 13:53:19 -05001381 rc = dquot_initialize(dir);
1382 if (rc)
1383 goto out;
Christoph Hellwig907f4552010-03-03 09:05:06 -05001384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if ((rc = get_UCSname(&dname, dentry)))
1386 goto out;
1387
1388 ip = ialloc(dir, mode);
Akinobu Mita087387f2006-09-14 09:22:38 -05001389 if (IS_ERR(ip)) {
1390 rc = PTR_ERR(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 goto out1;
1392 }
1393 jfs_ip = JFS_IP(ip);
1394
1395 tid = txBegin(dir->i_sb, 0);
1396
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001397 mutex_lock_nested(&JFS_IP(dir)->commit_mutex, COMMIT_MUTEX_PARENT);
1398 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001400 rc = jfs_init_acl(tid, ip, dir);
1401 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 goto out3;
1403
Eric Paris2a7dba32011-02-01 11:05:39 -05001404 rc = jfs_init_security(tid, ip, dir, &dentry->d_name);
Dave Kleikamp1d15b10f2005-09-01 09:05:39 -05001405 if (rc) {
1406 txAbort(tid, 0);
1407 goto out3;
1408 }
1409
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001410 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE))) {
1411 txAbort(tid, 0);
1412 goto out3;
1413 }
1414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 tblk = tid_to_tblock(tid);
1416 tblk->xflag |= COMMIT_CREATE;
1417 tblk->ino = ip->i_ino;
1418 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
1419
1420 ino = ip->i_ino;
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001421 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack))) {
1422 txAbort(tid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 goto out3;
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426 ip->i_op = &jfs_file_inode_operations;
1427 jfs_ip->dev = new_encode_dev(rdev);
1428 init_special_inode(ip, ip->i_mode, rdev);
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 mark_inode_dirty(ip);
1431
Deepa Dinamani078cd822016-09-14 07:48:04 -07001432 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 mark_inode_dirty(dir);
1435
1436 iplist[0] = dir;
1437 iplist[1] = ip;
1438 rc = txCommit(tid, 2, iplist, 0);
1439
1440 out3:
1441 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -06001442 mutex_unlock(&JFS_IP(ip)->commit_mutex);
1443 mutex_unlock(&JFS_IP(dir)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 if (rc) {
Dave Kleikamp4f4b4012005-09-01 09:02:43 -05001445 free_ea_wmap(ip);
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02001446 clear_nlink(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001447 unlock_new_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 iput(ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001449 } else {
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001450 unlock_new_inode(ip);
Al Viro8fc37ec2012-07-19 09:18:15 +04001451 d_instantiate(dentry, ip);
Dave Kleikamp1f3403f2008-12-30 22:08:37 -06001452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 out1:
1455 free_UCSname(&dname);
1456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 out:
1458 jfs_info("jfs_mknod: returning %d", rc);
1459 return rc;
1460}
1461
Al Viro00cd8dd2012-06-10 17:13:09 -04001462static struct dentry *jfs_lookup(struct inode *dip, struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
1464 struct btstack btstack;
1465 ino_t inum;
1466 struct inode *ip;
1467 struct component_name key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 int rc;
1469
Al Viroa4555892014-10-21 20:11:25 -04001470 jfs_info("jfs_lookup: name = %pd", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Al Viro79ac5a42011-07-17 10:17:46 -04001472 if ((rc = get_UCSname(&key, dentry)))
1473 return ERR_PTR(rc);
1474 rc = dtSearch(dip, &key, &inum, &btstack, JFS_LOOKUP);
1475 free_UCSname(&key);
1476 if (rc == -ENOENT) {
1477 ip = NULL;
1478 } else if (rc) {
1479 jfs_err("jfs_lookup: dtSearch returned %d", rc);
1480 ip = ERR_PTR(rc);
1481 } else {
1482 ip = jfs_iget(dip->i_sb, inum);
1483 if (IS_ERR(ip))
1484 jfs_err("jfs_lookup: iget failed on inum %d", (uint)inum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
1486
Al Viro94b77bd2010-12-18 10:59:31 -05001487 return d_splice_alias(ip, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488}
1489
Christoph Hellwigd425de72007-10-21 16:42:09 -07001490static struct inode *jfs_nfs_get_inode(struct super_block *sb,
1491 u64 ino, u32 generation)
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001492{
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001493 struct inode *inode;
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001494
1495 if (ino == 0)
1496 return ERR_PTR(-ESTALE);
David Howellseab1df72008-02-07 00:15:43 -08001497 inode = jfs_iget(sb, ino);
1498 if (IS_ERR(inode))
1499 return ERR_CAST(inode);
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001500
David Howellseab1df72008-02-07 00:15:43 -08001501 if (generation && inode->i_generation != generation) {
Christoph Hellwigd425de72007-10-21 16:42:09 -07001502 iput(inode);
1503 return ERR_PTR(-ESTALE);
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001504 }
1505
Christoph Hellwigd425de72007-10-21 16:42:09 -07001506 return inode;
1507}
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001508
Christoph Hellwigd425de72007-10-21 16:42:09 -07001509struct dentry *jfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
1510 int fh_len, int fh_type)
1511{
1512 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1513 jfs_nfs_get_inode);
1514}
1515
1516struct dentry *jfs_fh_to_parent(struct super_block *sb, struct fid *fid,
1517 int fh_len, int fh_type)
1518{
1519 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1520 jfs_nfs_get_inode);
Christoph Hellwig5ca29602007-07-17 04:04:29 -07001521}
1522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523struct dentry *jfs_get_parent(struct dentry *dentry)
1524{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 unsigned long parent_ino;
1526
1527 parent_ino =
David Howells2b0143b2015-03-17 22:25:59 +00001528 le32_to_cpu(JFS_IP(d_inode(dentry))->i_dtroot.header.idotdot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Al Virofc640052016-04-10 01:33:30 -04001530 return d_obtain_alias(jfs_iget(dentry->d_sb, parent_ino));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531}
1532
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001533const struct inode_operations jfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 .create = jfs_create,
1535 .lookup = jfs_lookup,
1536 .link = jfs_link,
1537 .unlink = jfs_unlink,
1538 .symlink = jfs_symlink,
1539 .mkdir = jfs_mkdir,
1540 .rmdir = jfs_rmdir,
1541 .mknod = jfs_mknod,
1542 .rename = jfs_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 .listxattr = jfs_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 .setattr = jfs_setattr,
Christoph Hellwig759bfee2010-03-03 09:05:02 -05001545#ifdef CONFIG_JFS_POSIX_ACL
Christoph Hellwig4e34e712011-07-23 17:37:31 +02001546 .get_acl = jfs_get_acl,
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -08001547 .set_acl = jfs_set_acl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548#endif
1549};
1550
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001551const struct file_operations jfs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 .read = generic_read_dir,
Al Viro070a0eb2013-05-17 17:00:34 -04001553 .iterate = jfs_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 .fsync = jfs_fsync,
Andi Kleenbaab81f2008-01-27 16:58:51 -06001555 .unlocked_ioctl = jfs_ioctl,
Andi Kleenef1fc2f2008-01-27 17:02:02 -06001556#ifdef CONFIG_COMPAT
1557 .compat_ioctl = jfs_compat_ioctl,
1558#endif
Christoph Hellwig3222a3e2008-09-03 21:53:01 +02001559 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560};
1561
Linus Torvaldsda53be12013-05-21 15:22:44 -07001562static int jfs_ci_hash(const struct dentry *dir, struct qstr *this)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
1564 unsigned long hash;
1565 int i;
1566
Linus Torvalds8387ff22016-06-10 07:51:30 -07001567 hash = init_name_hash(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 for (i=0; i < this->len; i++)
1569 hash = partial_name_hash(tolower(this->name[i]), hash);
1570 this->hash = end_name_hash(hash);
1571
1572 return 0;
1573}
1574
Al Viro6fa67e72016-07-31 16:37:25 -04001575static int jfs_ci_compare(const struct dentry *dentry,
Nick Piggin621e1552011-01-07 17:49:27 +11001576 unsigned int len, const char *str, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
1578 int i, result = 1;
1579
Nick Piggin621e1552011-01-07 17:49:27 +11001580 if (len != name->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 goto out;
Nick Piggin621e1552011-01-07 17:49:27 +11001582 for (i=0; i < len; i++) {
1583 if (tolower(str[i]) != tolower(name->name[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 goto out;
1585 }
1586 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587out:
1588 return result;
1589}
1590
Al Viro0b728e12012-06-10 16:03:43 -04001591static int jfs_ci_revalidate(struct dentry *dentry, unsigned int flags)
Nick Piggin2bc334d2011-01-07 17:49:25 +11001592{
1593 /*
1594 * This is not negative dentry. Always valid.
1595 *
1596 * Note, rename() to existing directory entry will have ->d_inode,
1597 * and will use existing name which isn't specified name by user.
1598 *
1599 * We may be able to drop this positive dentry here. But dropping
1600 * positive dentry isn't good idea. So it's unsupported like
1601 * rename("filename", "FILENAME") for now.
1602 */
David Howells2b0143b2015-03-17 22:25:59 +00001603 if (d_really_is_positive(dentry))
Nick Piggin2bc334d2011-01-07 17:49:25 +11001604 return 1;
1605
1606 /*
1607 * This may be nfsd (or something), anyway, we can't see the
1608 * intent of this. So, since this can be for creation, drop it.
1609 */
Al Viro0b728e12012-06-10 16:03:43 -04001610 if (!flags)
Nick Piggin2bc334d2011-01-07 17:49:25 +11001611 return 0;
1612
1613 /*
1614 * Drop the negative dentry, in order to make sure to use the
1615 * case sensitive name which is specified by user if this is
1616 * for creation.
1617 */
Al Viro0b728e12012-06-10 16:03:43 -04001618 if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
Al Viro407938e2011-06-25 21:37:18 -04001619 return 0;
Nick Piggin2bc334d2011-01-07 17:49:25 +11001620 return 1;
1621}
1622
Al Viroad28b4e2009-02-20 06:00:49 +00001623const struct dentry_operations jfs_ci_dentry_operations =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
1625 .d_hash = jfs_ci_hash,
1626 .d_compare = jfs_ci_compare,
Nick Piggin2bc334d2011-01-07 17:49:25 +11001627 .d_revalidate = jfs_ci_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628};