namei.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621
  1. /*
  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
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * 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
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/fs.h>
  20. #include <linux/namei.h>
  21. #include <linux/ctype.h>
  22. #include <linux/quotaops.h>
  23. #include <linux/exportfs.h>
  24. #include "jfs_incore.h"
  25. #include "jfs_superblock.h"
  26. #include "jfs_inode.h"
  27. #include "jfs_dinode.h"
  28. #include "jfs_dmap.h"
  29. #include "jfs_unicode.h"
  30. #include "jfs_metapage.h"
  31. #include "jfs_xattr.h"
  32. #include "jfs_acl.h"
  33. #include "jfs_debug.h"
  34. /*
  35. * forward references
  36. */
  37. const struct dentry_operations jfs_ci_dentry_operations;
  38. static s64 commitZeroLink(tid_t, struct inode *);
  39. /*
  40. * NAME: free_ea_wmap(inode)
  41. *
  42. * FUNCTION: free uncommitted extended attributes from working map
  43. *
  44. */
  45. static inline void free_ea_wmap(struct inode *inode)
  46. {
  47. dxd_t *ea = &JFS_IP(inode)->ea;
  48. if (ea->flag & DXD_EXTENT) {
  49. /* free EA pages from cache */
  50. invalidate_dxd_metapages(inode, *ea);
  51. dbFree(inode, addressDXD(ea), lengthDXD(ea));
  52. }
  53. ea->flag = 0;
  54. }
  55. /*
  56. * NAME: jfs_create(dip, dentry, mode)
  57. *
  58. * FUNCTION: create a regular file in the parent directory <dip>
  59. * with name = <from dentry> and mode = <mode>
  60. *
  61. * PARAMETER: dip - parent directory vnode
  62. * dentry - dentry of new file
  63. * mode - create mode (rwxrwxrwx).
  64. * nd- nd struct
  65. *
  66. * RETURN: Errors from subroutines
  67. *
  68. */
  69. static int jfs_create(struct inode *dip, struct dentry *dentry, umode_t mode,
  70. bool excl)
  71. {
  72. int rc = 0;
  73. tid_t tid; /* transaction id */
  74. struct inode *ip = NULL; /* child directory inode */
  75. ino_t ino;
  76. struct component_name dname; /* child directory name */
  77. struct btstack btstack;
  78. struct inode *iplist[2];
  79. struct tblock *tblk;
  80. jfs_info("jfs_create: dip:0x%p name:%pd", dip, dentry);
  81. rc = dquot_initialize(dip);
  82. if (rc)
  83. goto out1;
  84. /*
  85. * search parent directory for entry/freespace
  86. * (dtSearch() returns parent directory page pinned)
  87. */
  88. if ((rc = get_UCSname(&dname, dentry)))
  89. goto out1;
  90. /*
  91. * Either iAlloc() or txBegin() may block. Deadlock can occur if we
  92. * block there while holding dtree page, so we allocate the inode &
  93. * begin the transaction before we search the directory.
  94. */
  95. ip = ialloc(dip, mode);
  96. if (IS_ERR(ip)) {
  97. rc = PTR_ERR(ip);
  98. goto out2;
  99. }
  100. tid = txBegin(dip->i_sb, 0);
  101. mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
  102. mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
  103. rc = jfs_init_acl(tid, ip, dip);
  104. if (rc)
  105. goto out3;
  106. rc = jfs_init_security(tid, ip, dip, &dentry->d_name);
  107. if (rc) {
  108. txAbort(tid, 0);
  109. goto out3;
  110. }
  111. if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
  112. jfs_err("jfs_create: dtSearch returned %d", rc);
  113. txAbort(tid, 0);
  114. goto out3;
  115. }
  116. tblk = tid_to_tblock(tid);
  117. tblk->xflag |= COMMIT_CREATE;
  118. tblk->ino = ip->i_ino;
  119. tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
  120. iplist[0] = dip;
  121. iplist[1] = ip;
  122. /*
  123. * initialize the child XAD tree root in-line in inode
  124. */
  125. xtInitRoot(tid, ip);
  126. /*
  127. * create entry in parent directory for child directory
  128. * (dtInsert() releases parent directory page)
  129. */
  130. ino = ip->i_ino;
  131. if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
  132. if (rc == -EIO) {
  133. jfs_err("jfs_create: dtInsert returned -EIO");
  134. txAbort(tid, 1); /* Marks Filesystem dirty */
  135. } else
  136. txAbort(tid, 0); /* Filesystem full */
  137. goto out3;
  138. }
  139. ip->i_op = &jfs_file_inode_operations;
  140. ip->i_fop = &jfs_file_operations;
  141. ip->i_mapping->a_ops = &jfs_aops;
  142. mark_inode_dirty(ip);
  143. dip->i_ctime = dip->i_mtime = current_time(dip);
  144. mark_inode_dirty(dip);
  145. rc = txCommit(tid, 2, &iplist[0], 0);
  146. out3:
  147. txEnd(tid);
  148. mutex_unlock(&JFS_IP(ip)->commit_mutex);
  149. mutex_unlock(&JFS_IP(dip)->commit_mutex);
  150. if (rc) {
  151. free_ea_wmap(ip);
  152. clear_nlink(ip);
  153. discard_new_inode(ip);
  154. } else {
  155. d_instantiate_new(dentry, ip);
  156. }
  157. out2:
  158. free_UCSname(&dname);
  159. out1:
  160. jfs_info("jfs_create: rc:%d", rc);
  161. return rc;
  162. }
  163. /*
  164. * NAME: jfs_mkdir(dip, dentry, mode)
  165. *
  166. * FUNCTION: create a child directory in the parent directory <dip>
  167. * with name = <from dentry> and mode = <mode>
  168. *
  169. * PARAMETER: dip - parent directory vnode
  170. * dentry - dentry of child directory
  171. * mode - create mode (rwxrwxrwx).
  172. *
  173. * RETURN: Errors from subroutines
  174. *
  175. * note:
  176. * EACCESS: user needs search+write permission on the parent directory
  177. */
  178. static int jfs_mkdir(struct inode *dip, struct dentry *dentry, umode_t mode)
  179. {
  180. int rc = 0;
  181. tid_t tid; /* transaction id */
  182. struct inode *ip = NULL; /* child directory inode */
  183. ino_t ino;
  184. struct component_name dname; /* child directory name */
  185. struct btstack btstack;
  186. struct inode *iplist[2];
  187. struct tblock *tblk;
  188. jfs_info("jfs_mkdir: dip:0x%p name:%pd", dip, dentry);
  189. rc = dquot_initialize(dip);
  190. if (rc)
  191. goto out1;
  192. /*
  193. * search parent directory for entry/freespace
  194. * (dtSearch() returns parent directory page pinned)
  195. */
  196. if ((rc = get_UCSname(&dname, dentry)))
  197. goto out1;
  198. /*
  199. * Either iAlloc() or txBegin() may block. Deadlock can occur if we
  200. * block there while holding dtree page, so we allocate the inode &
  201. * begin the transaction before we search the directory.
  202. */
  203. ip = ialloc(dip, S_IFDIR | mode);
  204. if (IS_ERR(ip)) {
  205. rc = PTR_ERR(ip);
  206. goto out2;
  207. }
  208. tid = txBegin(dip->i_sb, 0);
  209. mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
  210. mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
  211. rc = jfs_init_acl(tid, ip, dip);
  212. if (rc)
  213. goto out3;
  214. rc = jfs_init_security(tid, ip, dip, &dentry->d_name);
  215. if (rc) {
  216. txAbort(tid, 0);
  217. goto out3;
  218. }
  219. if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
  220. jfs_err("jfs_mkdir: dtSearch returned %d", rc);
  221. txAbort(tid, 0);
  222. goto out3;
  223. }
  224. tblk = tid_to_tblock(tid);
  225. tblk->xflag |= COMMIT_CREATE;
  226. tblk->ino = ip->i_ino;
  227. tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
  228. iplist[0] = dip;
  229. iplist[1] = ip;
  230. /*
  231. * initialize the child directory in-line in inode
  232. */
  233. dtInitRoot(tid, ip, dip->i_ino);
  234. /*
  235. * create entry in parent directory for child directory
  236. * (dtInsert() releases parent directory page)
  237. */
  238. ino = ip->i_ino;
  239. if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
  240. if (rc == -EIO) {
  241. jfs_err("jfs_mkdir: dtInsert returned -EIO");
  242. txAbort(tid, 1); /* Marks Filesystem dirty */
  243. } else
  244. txAbort(tid, 0); /* Filesystem full */
  245. goto out3;
  246. }
  247. set_nlink(ip, 2); /* for '.' */
  248. ip->i_op = &jfs_dir_inode_operations;
  249. ip->i_fop = &jfs_dir_operations;
  250. mark_inode_dirty(ip);
  251. /* update parent directory inode */
  252. inc_nlink(dip); /* for '..' from child directory */
  253. dip->i_ctime = dip->i_mtime = current_time(dip);
  254. mark_inode_dirty(dip);
  255. rc = txCommit(tid, 2, &iplist[0], 0);
  256. out3:
  257. txEnd(tid);
  258. mutex_unlock(&JFS_IP(ip)->commit_mutex);
  259. mutex_unlock(&JFS_IP(dip)->commit_mutex);
  260. if (rc) {
  261. free_ea_wmap(ip);
  262. clear_nlink(ip);
  263. discard_new_inode(ip);
  264. } else {
  265. d_instantiate_new(dentry, ip);
  266. }
  267. out2:
  268. free_UCSname(&dname);
  269. out1:
  270. jfs_info("jfs_mkdir: rc:%d", rc);
  271. return rc;
  272. }
  273. /*
  274. * NAME: jfs_rmdir(dip, dentry)
  275. *
  276. * FUNCTION: remove a link to child directory
  277. *
  278. * PARAMETER: dip - parent inode
  279. * dentry - child directory dentry
  280. *
  281. * RETURN: -EINVAL - if name is . or ..
  282. * -EINVAL - if . or .. exist but are invalid.
  283. * errors from subroutines
  284. *
  285. * note:
  286. * if other threads have the directory open when the last link
  287. * is removed, the "." and ".." entries, if present, are removed before
  288. * rmdir() returns and no new entries may be created in the directory,
  289. * but the directory is not removed until the last reference to
  290. * the directory is released (cf.unlink() of regular file).
  291. */
  292. static int jfs_rmdir(struct inode *dip, struct dentry *dentry)
  293. {
  294. int rc;
  295. tid_t tid; /* transaction id */
  296. struct inode *ip = d_inode(dentry);
  297. ino_t ino;
  298. struct component_name dname;
  299. struct inode *iplist[2];
  300. struct tblock *tblk;
  301. jfs_info("jfs_rmdir: dip:0x%p name:%pd", dip, dentry);
  302. /* Init inode for quota operations. */
  303. rc = dquot_initialize(dip);
  304. if (rc)
  305. goto out;
  306. rc = dquot_initialize(ip);
  307. if (rc)
  308. goto out;
  309. /* directory must be empty to be removed */
  310. if (!dtEmpty(ip)) {
  311. rc = -ENOTEMPTY;
  312. goto out;
  313. }
  314. if ((rc = get_UCSname(&dname, dentry))) {
  315. goto out;
  316. }
  317. tid = txBegin(dip->i_sb, 0);
  318. mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
  319. mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
  320. iplist[0] = dip;
  321. iplist[1] = ip;
  322. tblk = tid_to_tblock(tid);
  323. tblk->xflag |= COMMIT_DELETE;
  324. tblk->u.ip = ip;
  325. /*
  326. * delete the entry of target directory from parent directory
  327. */
  328. ino = ip->i_ino;
  329. if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
  330. jfs_err("jfs_rmdir: dtDelete returned %d", rc);
  331. if (rc == -EIO)
  332. txAbort(tid, 1);
  333. txEnd(tid);
  334. mutex_unlock(&JFS_IP(ip)->commit_mutex);
  335. mutex_unlock(&JFS_IP(dip)->commit_mutex);
  336. goto out2;
  337. }
  338. /* update parent directory's link count corresponding
  339. * to ".." entry of the target directory deleted
  340. */
  341. dip->i_ctime = dip->i_mtime = current_time(dip);
  342. inode_dec_link_count(dip);
  343. /*
  344. * OS/2 could have created EA and/or ACL
  345. */
  346. /* free EA from both persistent and working map */
  347. if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
  348. /* free EA pages */
  349. txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
  350. }
  351. JFS_IP(ip)->ea.flag = 0;
  352. /* free ACL from both persistent and working map */
  353. if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
  354. /* free ACL pages */
  355. txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
  356. }
  357. JFS_IP(ip)->acl.flag = 0;
  358. /* mark the target directory as deleted */
  359. clear_nlink(ip);
  360. mark_inode_dirty(ip);
  361. rc = txCommit(tid, 2, &iplist[0], 0);
  362. txEnd(tid);
  363. mutex_unlock(&JFS_IP(ip)->commit_mutex);
  364. mutex_unlock(&JFS_IP(dip)->commit_mutex);
  365. /*
  366. * Truncating the directory index table is not guaranteed. It
  367. * may need to be done iteratively
  368. */
  369. if (test_cflag(COMMIT_Stale, dip)) {
  370. if (dip->i_size > 1)
  371. jfs_truncate_nolock(dip, 0);
  372. clear_cflag(COMMIT_Stale, dip);
  373. }
  374. out2:
  375. free_UCSname(&dname);
  376. out:
  377. jfs_info("jfs_rmdir: rc:%d", rc);
  378. return rc;
  379. }
  380. /*
  381. * NAME: jfs_unlink(dip, dentry)
  382. *
  383. * FUNCTION: remove a link to object <vp> named by <name>
  384. * from parent directory <dvp>
  385. *
  386. * PARAMETER: dip - inode of parent directory
  387. * dentry - dentry of object to be removed
  388. *
  389. * RETURN: errors from subroutines
  390. *
  391. * note:
  392. * temporary file: if one or more processes have the file open
  393. * when the last link is removed, the link will be removed before
  394. * unlink() returns, but the removal of the file contents will be
  395. * postponed until all references to the files are closed.
  396. *
  397. * JFS does NOT support unlink() on directories.
  398. *
  399. */
  400. static int jfs_unlink(struct inode *dip, struct dentry *dentry)
  401. {
  402. int rc;
  403. tid_t tid; /* transaction id */
  404. struct inode *ip = d_inode(dentry);
  405. ino_t ino;
  406. struct component_name dname; /* object name */
  407. struct inode *iplist[2];
  408. struct tblock *tblk;
  409. s64 new_size = 0;
  410. int commit_flag;
  411. jfs_info("jfs_unlink: dip:0x%p name:%pd", dip, dentry);
  412. /* Init inode for quota operations. */
  413. rc = dquot_initialize(dip);
  414. if (rc)
  415. goto out;
  416. rc = dquot_initialize(ip);
  417. if (rc)
  418. goto out;
  419. if ((rc = get_UCSname(&dname, dentry)))
  420. goto out;
  421. IWRITE_LOCK(ip, RDWRLOCK_NORMAL);
  422. tid = txBegin(dip->i_sb, 0);
  423. mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
  424. mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
  425. iplist[0] = dip;
  426. iplist[1] = ip;
  427. /*
  428. * delete the entry of target file from parent directory
  429. */
  430. ino = ip->i_ino;
  431. if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
  432. jfs_err("jfs_unlink: dtDelete returned %d", rc);
  433. if (rc == -EIO)
  434. txAbort(tid, 1); /* Marks FS Dirty */
  435. txEnd(tid);
  436. mutex_unlock(&JFS_IP(ip)->commit_mutex);
  437. mutex_unlock(&JFS_IP(dip)->commit_mutex);
  438. IWRITE_UNLOCK(ip);
  439. goto out1;
  440. }
  441. ASSERT(ip->i_nlink);
  442. ip->i_ctime = dip->i_ctime = dip->i_mtime = current_time(ip);
  443. mark_inode_dirty(dip);
  444. /* update target's inode */
  445. inode_dec_link_count(ip);
  446. /*
  447. * commit zero link count object
  448. */
  449. if (ip->i_nlink == 0) {
  450. assert(!test_cflag(COMMIT_Nolink, ip));
  451. /* free block resources */
  452. if ((new_size = commitZeroLink(tid, ip)) < 0) {
  453. txAbort(tid, 1); /* Marks FS Dirty */
  454. txEnd(tid);
  455. mutex_unlock(&JFS_IP(ip)->commit_mutex);
  456. mutex_unlock(&JFS_IP(dip)->commit_mutex);
  457. IWRITE_UNLOCK(ip);
  458. rc = new_size;
  459. goto out1;
  460. }
  461. tblk = tid_to_tblock(tid);
  462. tblk->xflag |= COMMIT_DELETE;
  463. tblk->u.ip = ip;
  464. }
  465. /*
  466. * Incomplete truncate of file data can
  467. * result in timing problems unless we synchronously commit the
  468. * transaction.
  469. */
  470. if (new_size)
  471. commit_flag = COMMIT_SYNC;
  472. else
  473. commit_flag = 0;
  474. /*
  475. * If xtTruncate was incomplete, commit synchronously to avoid
  476. * timing complications
  477. */
  478. rc = txCommit(tid, 2, &iplist[0], commit_flag);
  479. txEnd(tid);
  480. mutex_unlock(&JFS_IP(ip)->commit_mutex);
  481. mutex_unlock(&JFS_IP(dip)->commit_mutex);
  482. while (new_size && (rc == 0)) {
  483. tid = txBegin(dip->i_sb, 0);
  484. mutex_lock(&JFS_IP(ip)->commit_mutex);
  485. new_size = xtTruncate_pmap(tid, ip, new_size);
  486. if (new_size < 0) {
  487. txAbort(tid, 1); /* Marks FS Dirty */
  488. rc = new_size;
  489. } else
  490. rc = txCommit(tid, 2, &iplist[0], COMMIT_SYNC);
  491. txEnd(tid);
  492. mutex_unlock(&JFS_IP(ip)->commit_mutex);
  493. }
  494. if (ip->i_nlink == 0)
  495. set_cflag(COMMIT_Nolink, ip);
  496. IWRITE_UNLOCK(ip);
  497. /*
  498. * Truncating the directory index table is not guaranteed. It
  499. * may need to be done iteratively
  500. */
  501. if (test_cflag(COMMIT_Stale, dip)) {
  502. if (dip->i_size > 1)
  503. jfs_truncate_nolock(dip, 0);
  504. clear_cflag(COMMIT_Stale, dip);
  505. }
  506. out1:
  507. free_UCSname(&dname);
  508. out:
  509. jfs_info("jfs_unlink: rc:%d", rc);
  510. return rc;
  511. }
  512. /*
  513. * NAME: commitZeroLink()
  514. *
  515. * FUNCTION: for non-directory, called by jfs_remove(),
  516. * truncate a regular file, directory or symbolic
  517. * link to zero length. return 0 if type is not
  518. * one of these.
  519. *
  520. * if the file is currently associated with a VM segment
  521. * only permanent disk and inode map resources are freed,
  522. * and neither the inode nor indirect blocks are modified
  523. * so that the resources can be later freed in the work
  524. * map by ctrunc1.
  525. * if there is no VM segment on entry, the resources are
  526. * freed in both work and permanent map.
  527. * (? for temporary file - memory object is cached even
  528. * after no reference:
  529. * reference count > 0 - )
  530. *
  531. * PARAMETERS: cd - pointer to commit data structure.
  532. * current inode is the one to truncate.
  533. *
  534. * RETURN: Errors from subroutines
  535. */
  536. static s64 commitZeroLink(tid_t tid, struct inode *ip)
  537. {
  538. int filetype;
  539. struct tblock *tblk;
  540. jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid, ip);
  541. filetype = ip->i_mode & S_IFMT;
  542. switch (filetype) {
  543. case S_IFREG:
  544. break;
  545. case S_IFLNK:
  546. /* fast symbolic link */
  547. if (ip->i_size < IDATASIZE) {
  548. ip->i_size = 0;
  549. return 0;
  550. }
  551. break;
  552. default:
  553. assert(filetype != S_IFDIR);
  554. return 0;
  555. }
  556. set_cflag(COMMIT_Freewmap, ip);
  557. /* mark transaction of block map update type */
  558. tblk = tid_to_tblock(tid);
  559. tblk->xflag |= COMMIT_PMAP;
  560. /*
  561. * free EA
  562. */
  563. if (JFS_IP(ip)->ea.flag & DXD_EXTENT)
  564. /* acquire maplock on EA to be freed from block map */
  565. txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
  566. /*
  567. * free ACL
  568. */
  569. if (JFS_IP(ip)->acl.flag & DXD_EXTENT)
  570. /* acquire maplock on EA to be freed from block map */
  571. txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
  572. /*
  573. * free xtree/data (truncate to zero length):
  574. * free xtree/data pages from cache if COMMIT_PWMAP,
  575. * free xtree/data blocks from persistent block map, and
  576. * free xtree/data blocks from working block map if COMMIT_PWMAP;
  577. */
  578. if (ip->i_size)
  579. return xtTruncate_pmap(tid, ip, 0);
  580. return 0;
  581. }
  582. /*
  583. * NAME: jfs_free_zero_link()
  584. *
  585. * FUNCTION: for non-directory, called by iClose(),
  586. * free resources of a file from cache and WORKING map
  587. * for a file previously committed with zero link count
  588. * while associated with a pager object,
  589. *
  590. * PARAMETER: ip - pointer to inode of file.
  591. */
  592. void jfs_free_zero_link(struct inode *ip)
  593. {
  594. int type;
  595. jfs_info("jfs_free_zero_link: ip = 0x%p", ip);
  596. /* return if not reg or symbolic link or if size is
  597. * already ok.
  598. */
  599. type = ip->i_mode & S_IFMT;
  600. switch (type) {
  601. case S_IFREG:
  602. break;
  603. case S_IFLNK:
  604. /* if its contained in inode nothing to do */
  605. if (ip->i_size < IDATASIZE)
  606. return;
  607. break;
  608. default:
  609. return;
  610. }
  611. /*
  612. * free EA
  613. */
  614. if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
  615. s64 xaddr = addressDXD(&JFS_IP(ip)->ea);
  616. int xlen = lengthDXD(&JFS_IP(ip)->ea);
  617. struct maplock maplock; /* maplock for COMMIT_WMAP */
  618. struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
  619. /* free EA pages from cache */
  620. invalidate_dxd_metapages(ip, JFS_IP(ip)->ea);
  621. /* free EA extent from working block map */
  622. maplock.index = 1;
  623. pxdlock = (struct pxd_lock *) & maplock;
  624. pxdlock->flag = mlckFREEPXD;
  625. PXDaddress(&pxdlock->pxd, xaddr);
  626. PXDlength(&pxdlock->pxd, xlen);
  627. txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
  628. }
  629. /*
  630. * free ACL
  631. */
  632. if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
  633. s64 xaddr = addressDXD(&JFS_IP(ip)->acl);
  634. int xlen = lengthDXD(&JFS_IP(ip)->acl);
  635. struct maplock maplock; /* maplock for COMMIT_WMAP */
  636. struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
  637. invalidate_dxd_metapages(ip, JFS_IP(ip)->acl);
  638. /* free ACL extent from working block map */
  639. maplock.index = 1;
  640. pxdlock = (struct pxd_lock *) & maplock;
  641. pxdlock->flag = mlckFREEPXD;
  642. PXDaddress(&pxdlock->pxd, xaddr);
  643. PXDlength(&pxdlock->pxd, xlen);
  644. txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
  645. }
  646. /*
  647. * free xtree/data (truncate to zero length):
  648. * free xtree/data pages from cache, and
  649. * free xtree/data blocks from working block map;
  650. */
  651. if (ip->i_size)
  652. xtTruncate(0, ip, 0, COMMIT_WMAP);
  653. }
  654. /*
  655. * NAME: jfs_link(vp, dvp, name, crp)
  656. *
  657. * FUNCTION: create a link to <vp> by the name = <name>
  658. * in the parent directory <dvp>
  659. *
  660. * PARAMETER: vp - target object
  661. * dvp - parent directory of new link
  662. * name - name of new link to target object
  663. * crp - credential
  664. *
  665. * RETURN: Errors from subroutines
  666. *
  667. * note:
  668. * JFS does NOT support link() on directories (to prevent circular
  669. * path in the directory hierarchy);
  670. * EPERM: the target object is a directory, and either the caller
  671. * does not have appropriate privileges or the implementation prohibits
  672. * using link() on directories [XPG4.2].
  673. *
  674. * JFS does NOT support links between file systems:
  675. * EXDEV: target object and new link are on different file systems and
  676. * implementation does not support links between file systems [XPG4.2].
  677. */
  678. static int jfs_link(struct dentry *old_dentry,
  679. struct inode *dir, struct dentry *dentry)
  680. {
  681. int rc;
  682. tid_t tid;
  683. struct inode *ip = d_inode(old_dentry);
  684. ino_t ino;
  685. struct component_name dname;
  686. struct btstack btstack;
  687. struct inode *iplist[2];
  688. jfs_info("jfs_link: %pd %pd", old_dentry, dentry);
  689. rc = dquot_initialize(dir);
  690. if (rc)
  691. goto out;
  692. tid = txBegin(ip->i_sb, 0);
  693. mutex_lock_nested(&JFS_IP(dir)->commit_mutex, COMMIT_MUTEX_PARENT);
  694. mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
  695. /*
  696. * scan parent directory for entry/freespace
  697. */
  698. if ((rc = get_UCSname(&dname, dentry)))
  699. goto out_tx;
  700. if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
  701. goto free_dname;
  702. /*
  703. * create entry for new link in parent directory
  704. */
  705. ino = ip->i_ino;
  706. if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
  707. goto free_dname;
  708. /* update object inode */
  709. inc_nlink(ip); /* for new link */
  710. ip->i_ctime = current_time(ip);
  711. dir->i_ctime = dir->i_mtime = current_time(dir);
  712. mark_inode_dirty(dir);
  713. ihold(ip);
  714. iplist[0] = ip;
  715. iplist[1] = dir;
  716. rc = txCommit(tid, 2, &iplist[0], 0);
  717. if (rc) {
  718. drop_nlink(ip); /* never instantiated */
  719. iput(ip);
  720. } else
  721. d_instantiate(dentry, ip);
  722. free_dname:
  723. free_UCSname(&dname);
  724. out_tx:
  725. txEnd(tid);
  726. mutex_unlock(&JFS_IP(ip)->commit_mutex);
  727. mutex_unlock(&JFS_IP(dir)->commit_mutex);
  728. out:
  729. jfs_info("jfs_link: rc:%d", rc);
  730. return rc;
  731. }
  732. /*
  733. * NAME: jfs_symlink(dip, dentry, name)
  734. *
  735. * FUNCTION: creates a symbolic link to <symlink> by name <name>
  736. * in directory <dip>
  737. *
  738. * PARAMETER: dip - parent directory vnode
  739. * dentry - dentry of symbolic link
  740. * name - the path name of the existing object
  741. * that will be the source of the link
  742. *
  743. * RETURN: errors from subroutines
  744. *
  745. * note:
  746. * ENAMETOOLONG: pathname resolution of a symbolic link produced
  747. * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
  748. */
  749. static int jfs_symlink(struct inode *dip, struct dentry *dentry,
  750. const char *name)
  751. {
  752. int rc;
  753. tid_t tid;
  754. ino_t ino = 0;
  755. struct component_name dname;
  756. int ssize; /* source pathname size */
  757. struct btstack btstack;
  758. struct inode *ip = d_inode(dentry);
  759. s64 xlen = 0;
  760. int bmask = 0, xsize;
  761. s64 xaddr;
  762. struct metapage *mp;
  763. struct super_block *sb;
  764. struct tblock *tblk;
  765. struct inode *iplist[2];
  766. jfs_info("jfs_symlink: dip:0x%p name:%s", dip, name);
  767. rc = dquot_initialize(dip);
  768. if (rc)
  769. goto out1;
  770. ssize = strlen(name) + 1;
  771. /*
  772. * search parent directory for entry/freespace
  773. * (dtSearch() returns parent directory page pinned)
  774. */
  775. if ((rc = get_UCSname(&dname, dentry)))
  776. goto out1;
  777. /*
  778. * allocate on-disk/in-memory inode for symbolic link:
  779. * (iAlloc() returns new, locked inode)
  780. */
  781. ip = ialloc(dip, S_IFLNK | 0777);
  782. if (IS_ERR(ip)) {
  783. rc = PTR_ERR(ip);
  784. goto out2;
  785. }
  786. tid = txBegin(dip->i_sb, 0);
  787. mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
  788. mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
  789. rc = jfs_init_security(tid, ip, dip, &dentry->d_name);
  790. if (rc)
  791. goto out3;
  792. tblk = tid_to_tblock(tid);
  793. tblk->xflag |= COMMIT_CREATE;
  794. tblk->ino = ip->i_ino;
  795. tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
  796. /* fix symlink access permission
  797. * (dir_create() ANDs in the u.u_cmask,
  798. * but symlinks really need to be 777 access)
  799. */
  800. ip->i_mode |= 0777;
  801. /*
  802. * write symbolic link target path name
  803. */
  804. xtInitRoot(tid, ip);
  805. /*
  806. * write source path name inline in on-disk inode (fast symbolic link)
  807. */
  808. if (ssize <= IDATASIZE) {
  809. ip->i_op = &jfs_fast_symlink_inode_operations;
  810. ip->i_link = JFS_IP(ip)->i_inline;
  811. memcpy(ip->i_link, name, ssize);
  812. ip->i_size = ssize - 1;
  813. /*
  814. * if symlink is > 128 bytes, we don't have the space to
  815. * store inline extended attributes
  816. */
  817. if (ssize > sizeof (JFS_IP(ip)->i_inline))
  818. JFS_IP(ip)->mode2 &= ~INLINEEA;
  819. jfs_info("jfs_symlink: fast symlink added ssize:%d name:%s ",
  820. ssize, name);
  821. }
  822. /*
  823. * write source path name in a single extent
  824. */
  825. else {
  826. jfs_info("jfs_symlink: allocate extent ip:0x%p", ip);
  827. ip->i_op = &jfs_symlink_inode_operations;
  828. inode_nohighmem(ip);
  829. ip->i_mapping->a_ops = &jfs_aops;
  830. /*
  831. * even though the data of symlink object (source
  832. * path name) is treated as non-journaled user data,
  833. * it is read/written thru buffer cache for performance.
  834. */
  835. sb = ip->i_sb;
  836. bmask = JFS_SBI(sb)->bsize - 1;
  837. xsize = (ssize + bmask) & ~bmask;
  838. xaddr = 0;
  839. xlen = xsize >> JFS_SBI(sb)->l2bsize;
  840. if ((rc = xtInsert(tid, ip, 0, 0, xlen, &xaddr, 0))) {
  841. txAbort(tid, 0);
  842. goto out3;
  843. }
  844. ip->i_size = ssize - 1;
  845. while (ssize) {
  846. /* This is kind of silly since PATH_MAX == 4K */
  847. int copy_size = min(ssize, PSIZE);
  848. mp = get_metapage(ip, xaddr, PSIZE, 1);
  849. if (mp == NULL) {
  850. xtTruncate(tid, ip, 0, COMMIT_PWMAP);
  851. rc = -EIO;
  852. txAbort(tid, 0);
  853. goto out3;
  854. }
  855. memcpy(mp->data, name, copy_size);
  856. flush_metapage(mp);
  857. ssize -= copy_size;
  858. name += copy_size;
  859. xaddr += JFS_SBI(sb)->nbperpage;
  860. }
  861. }
  862. /*
  863. * create entry for symbolic link in parent directory
  864. */
  865. rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE);
  866. if (rc == 0) {
  867. ino = ip->i_ino;
  868. rc = dtInsert(tid, dip, &dname, &ino, &btstack);
  869. }
  870. if (rc) {
  871. if (xlen)
  872. xtTruncate(tid, ip, 0, COMMIT_PWMAP);
  873. txAbort(tid, 0);
  874. /* discard new inode */
  875. goto out3;
  876. }
  877. mark_inode_dirty(ip);
  878. dip->i_ctime = dip->i_mtime = current_time(dip);
  879. mark_inode_dirty(dip);
  880. /*
  881. * commit update of parent directory and link object
  882. */
  883. iplist[0] = dip;
  884. iplist[1] = ip;
  885. rc = txCommit(tid, 2, &iplist[0], 0);
  886. out3:
  887. txEnd(tid);
  888. mutex_unlock(&JFS_IP(ip)->commit_mutex);
  889. mutex_unlock(&JFS_IP(dip)->commit_mutex);
  890. if (rc) {
  891. free_ea_wmap(ip);
  892. clear_nlink(ip);
  893. discard_new_inode(ip);
  894. } else {
  895. d_instantiate_new(dentry, ip);
  896. }
  897. out2:
  898. free_UCSname(&dname);
  899. out1:
  900. jfs_info("jfs_symlink: rc:%d", rc);
  901. return rc;
  902. }
  903. /*
  904. * NAME: jfs_rename
  905. *
  906. * FUNCTION: rename a file or directory
  907. */
  908. static int jfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  909. struct inode *new_dir, struct dentry *new_dentry,
  910. unsigned int flags)
  911. {
  912. struct btstack btstack;
  913. ino_t ino;
  914. struct component_name new_dname;
  915. struct inode *new_ip;
  916. struct component_name old_dname;
  917. struct inode *old_ip;
  918. int rc;
  919. tid_t tid;
  920. struct tlock *tlck;
  921. struct dt_lock *dtlck;
  922. struct lv *lv;
  923. int ipcount;
  924. struct inode *iplist[4];
  925. struct tblock *tblk;
  926. s64 new_size = 0;
  927. int commit_flag;
  928. if (flags & ~RENAME_NOREPLACE)
  929. return -EINVAL;
  930. jfs_info("jfs_rename: %pd %pd", old_dentry, new_dentry);
  931. rc = dquot_initialize(old_dir);
  932. if (rc)
  933. goto out1;
  934. rc = dquot_initialize(new_dir);
  935. if (rc)
  936. goto out1;
  937. old_ip = d_inode(old_dentry);
  938. new_ip = d_inode(new_dentry);
  939. if ((rc = get_UCSname(&old_dname, old_dentry)))
  940. goto out1;
  941. if ((rc = get_UCSname(&new_dname, new_dentry)))
  942. goto out2;
  943. /*
  944. * Make sure source inode number is what we think it is
  945. */
  946. rc = dtSearch(old_dir, &old_dname, &ino, &btstack, JFS_LOOKUP);
  947. if (rc || (ino != old_ip->i_ino)) {
  948. rc = -ENOENT;
  949. goto out3;
  950. }
  951. /*
  952. * Make sure dest inode number (if any) is what we think it is
  953. */
  954. rc = dtSearch(new_dir, &new_dname, &ino, &btstack, JFS_LOOKUP);
  955. if (!rc) {
  956. if ((!new_ip) || (ino != new_ip->i_ino)) {
  957. rc = -ESTALE;
  958. goto out3;
  959. }
  960. } else if (rc != -ENOENT)
  961. goto out3;
  962. else if (new_ip) {
  963. /* no entry exists, but one was expected */
  964. rc = -ESTALE;
  965. goto out3;
  966. }
  967. if (S_ISDIR(old_ip->i_mode)) {
  968. if (new_ip) {
  969. if (!dtEmpty(new_ip)) {
  970. rc = -ENOTEMPTY;
  971. goto out3;
  972. }
  973. }
  974. } else if (new_ip) {
  975. IWRITE_LOCK(new_ip, RDWRLOCK_NORMAL);
  976. /* Init inode for quota operations. */
  977. rc = dquot_initialize(new_ip);
  978. if (rc)
  979. goto out_unlock;
  980. }
  981. /*
  982. * The real work starts here
  983. */
  984. tid = txBegin(new_dir->i_sb, 0);
  985. /*
  986. * How do we know the locking is safe from deadlocks?
  987. * The vfs does the hard part for us. Any time we are taking nested
  988. * commit_mutexes, the vfs already has i_mutex held on the parent.
  989. * Here, the vfs has already taken i_mutex on both old_dir and new_dir.
  990. */
  991. mutex_lock_nested(&JFS_IP(new_dir)->commit_mutex, COMMIT_MUTEX_PARENT);
  992. mutex_lock_nested(&JFS_IP(old_ip)->commit_mutex, COMMIT_MUTEX_CHILD);
  993. if (old_dir != new_dir)
  994. mutex_lock_nested(&JFS_IP(old_dir)->commit_mutex,
  995. COMMIT_MUTEX_SECOND_PARENT);
  996. if (new_ip) {
  997. mutex_lock_nested(&JFS_IP(new_ip)->commit_mutex,
  998. COMMIT_MUTEX_VICTIM);
  999. /*
  1000. * Change existing directory entry to new inode number
  1001. */
  1002. ino = new_ip->i_ino;
  1003. rc = dtModify(tid, new_dir, &new_dname, &ino,
  1004. old_ip->i_ino, JFS_RENAME);
  1005. if (rc)
  1006. goto out_tx;
  1007. drop_nlink(new_ip);
  1008. if (S_ISDIR(new_ip->i_mode)) {
  1009. drop_nlink(new_ip);
  1010. if (new_ip->i_nlink) {
  1011. mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
  1012. if (old_dir != new_dir)
  1013. mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
  1014. mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
  1015. mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
  1016. if (!S_ISDIR(old_ip->i_mode) && new_ip)
  1017. IWRITE_UNLOCK(new_ip);
  1018. jfs_error(new_ip->i_sb,
  1019. "new_ip->i_nlink != 0\n");
  1020. return -EIO;
  1021. }
  1022. tblk = tid_to_tblock(tid);
  1023. tblk->xflag |= COMMIT_DELETE;
  1024. tblk->u.ip = new_ip;
  1025. } else if (new_ip->i_nlink == 0) {
  1026. assert(!test_cflag(COMMIT_Nolink, new_ip));
  1027. /* free block resources */
  1028. if ((new_size = commitZeroLink(tid, new_ip)) < 0) {
  1029. txAbort(tid, 1); /* Marks FS Dirty */
  1030. rc = new_size;
  1031. goto out_tx;
  1032. }
  1033. tblk = tid_to_tblock(tid);
  1034. tblk->xflag |= COMMIT_DELETE;
  1035. tblk->u.ip = new_ip;
  1036. } else {
  1037. new_ip->i_ctime = current_time(new_ip);
  1038. mark_inode_dirty(new_ip);
  1039. }
  1040. } else {
  1041. /*
  1042. * Add new directory entry
  1043. */
  1044. rc = dtSearch(new_dir, &new_dname, &ino, &btstack,
  1045. JFS_CREATE);
  1046. if (rc) {
  1047. jfs_err("jfs_rename didn't expect dtSearch to fail w/rc = %d",
  1048. rc);
  1049. goto out_tx;
  1050. }
  1051. ino = old_ip->i_ino;
  1052. rc = dtInsert(tid, new_dir, &new_dname, &ino, &btstack);
  1053. if (rc) {
  1054. if (rc == -EIO)
  1055. jfs_err("jfs_rename: dtInsert returned -EIO");
  1056. goto out_tx;
  1057. }
  1058. if (S_ISDIR(old_ip->i_mode))
  1059. inc_nlink(new_dir);
  1060. }
  1061. /*
  1062. * Remove old directory entry
  1063. */
  1064. ino = old_ip->i_ino;
  1065. rc = dtDelete(tid, old_dir, &old_dname, &ino, JFS_REMOVE);
  1066. if (rc) {
  1067. jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
  1068. rc);
  1069. txAbort(tid, 1); /* Marks Filesystem dirty */
  1070. goto out_tx;
  1071. }
  1072. if (S_ISDIR(old_ip->i_mode)) {
  1073. drop_nlink(old_dir);
  1074. if (old_dir != new_dir) {
  1075. /*
  1076. * Change inode number of parent for moved directory
  1077. */
  1078. JFS_IP(old_ip)->i_dtroot.header.idotdot =
  1079. cpu_to_le32(new_dir->i_ino);
  1080. /* Linelock header of dtree */
  1081. tlck = txLock(tid, old_ip,
  1082. (struct metapage *) &JFS_IP(old_ip)->bxflag,
  1083. tlckDTREE | tlckBTROOT | tlckRELINK);
  1084. dtlck = (struct dt_lock *) & tlck->lock;
  1085. ASSERT(dtlck->index == 0);
  1086. lv = & dtlck->lv[0];
  1087. lv->offset = 0;
  1088. lv->length = 1;
  1089. dtlck->index++;
  1090. }
  1091. }
  1092. /*
  1093. * Update ctime on changed/moved inodes & mark dirty
  1094. */
  1095. old_ip->i_ctime = current_time(old_ip);
  1096. mark_inode_dirty(old_ip);
  1097. new_dir->i_ctime = new_dir->i_mtime = current_time(new_dir);
  1098. mark_inode_dirty(new_dir);
  1099. /* Build list of inodes modified by this transaction */
  1100. ipcount = 0;
  1101. iplist[ipcount++] = old_ip;
  1102. if (new_ip)
  1103. iplist[ipcount++] = new_ip;
  1104. iplist[ipcount++] = old_dir;
  1105. if (old_dir != new_dir) {
  1106. iplist[ipcount++] = new_dir;
  1107. old_dir->i_ctime = old_dir->i_mtime = current_time(old_dir);
  1108. mark_inode_dirty(old_dir);
  1109. }
  1110. /*
  1111. * Incomplete truncate of file data can
  1112. * result in timing problems unless we synchronously commit the
  1113. * transaction.
  1114. */
  1115. if (new_size)
  1116. commit_flag = COMMIT_SYNC;
  1117. else
  1118. commit_flag = 0;
  1119. rc = txCommit(tid, ipcount, iplist, commit_flag);
  1120. out_tx:
  1121. txEnd(tid);
  1122. if (new_ip)
  1123. mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
  1124. if (old_dir != new_dir)
  1125. mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
  1126. mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
  1127. mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
  1128. while (new_size && (rc == 0)) {
  1129. tid = txBegin(new_ip->i_sb, 0);
  1130. mutex_lock(&JFS_IP(new_ip)->commit_mutex);
  1131. new_size = xtTruncate_pmap(tid, new_ip, new_size);
  1132. if (new_size < 0) {
  1133. txAbort(tid, 1);
  1134. rc = new_size;
  1135. } else
  1136. rc = txCommit(tid, 1, &new_ip, COMMIT_SYNC);
  1137. txEnd(tid);
  1138. mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
  1139. }
  1140. if (new_ip && (new_ip->i_nlink == 0))
  1141. set_cflag(COMMIT_Nolink, new_ip);
  1142. /*
  1143. * Truncating the directory index table is not guaranteed. It
  1144. * may need to be done iteratively
  1145. */
  1146. if (test_cflag(COMMIT_Stale, old_dir)) {
  1147. if (old_dir->i_size > 1)
  1148. jfs_truncate_nolock(old_dir, 0);
  1149. clear_cflag(COMMIT_Stale, old_dir);
  1150. }
  1151. out_unlock:
  1152. if (new_ip && !S_ISDIR(new_ip->i_mode))
  1153. IWRITE_UNLOCK(new_ip);
  1154. out3:
  1155. free_UCSname(&new_dname);
  1156. out2:
  1157. free_UCSname(&old_dname);
  1158. out1:
  1159. jfs_info("jfs_rename: returning %d", rc);
  1160. return rc;
  1161. }
  1162. /*
  1163. * NAME: jfs_mknod
  1164. *
  1165. * FUNCTION: Create a special file (device)
  1166. */
  1167. static int jfs_mknod(struct inode *dir, struct dentry *dentry,
  1168. umode_t mode, dev_t rdev)
  1169. {
  1170. struct jfs_inode_info *jfs_ip;
  1171. struct btstack btstack;
  1172. struct component_name dname;
  1173. ino_t ino;
  1174. struct inode *ip;
  1175. struct inode *iplist[2];
  1176. int rc;
  1177. tid_t tid;
  1178. struct tblock *tblk;
  1179. jfs_info("jfs_mknod: %pd", dentry);
  1180. rc = dquot_initialize(dir);
  1181. if (rc)
  1182. goto out;
  1183. if ((rc = get_UCSname(&dname, dentry)))
  1184. goto out;
  1185. ip = ialloc(dir, mode);
  1186. if (IS_ERR(ip)) {
  1187. rc = PTR_ERR(ip);
  1188. goto out1;
  1189. }
  1190. jfs_ip = JFS_IP(ip);
  1191. tid = txBegin(dir->i_sb, 0);
  1192. mutex_lock_nested(&JFS_IP(dir)->commit_mutex, COMMIT_MUTEX_PARENT);
  1193. mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
  1194. rc = jfs_init_acl(tid, ip, dir);
  1195. if (rc)
  1196. goto out3;
  1197. rc = jfs_init_security(tid, ip, dir, &dentry->d_name);
  1198. if (rc) {
  1199. txAbort(tid, 0);
  1200. goto out3;
  1201. }
  1202. if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE))) {
  1203. txAbort(tid, 0);
  1204. goto out3;
  1205. }
  1206. tblk = tid_to_tblock(tid);
  1207. tblk->xflag |= COMMIT_CREATE;
  1208. tblk->ino = ip->i_ino;
  1209. tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
  1210. ino = ip->i_ino;
  1211. if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack))) {
  1212. txAbort(tid, 0);
  1213. goto out3;
  1214. }
  1215. ip->i_op = &jfs_file_inode_operations;
  1216. jfs_ip->dev = new_encode_dev(rdev);
  1217. init_special_inode(ip, ip->i_mode, rdev);
  1218. mark_inode_dirty(ip);
  1219. dir->i_ctime = dir->i_mtime = current_time(dir);
  1220. mark_inode_dirty(dir);
  1221. iplist[0] = dir;
  1222. iplist[1] = ip;
  1223. rc = txCommit(tid, 2, iplist, 0);
  1224. out3:
  1225. txEnd(tid);
  1226. mutex_unlock(&JFS_IP(ip)->commit_mutex);
  1227. mutex_unlock(&JFS_IP(dir)->commit_mutex);
  1228. if (rc) {
  1229. free_ea_wmap(ip);
  1230. clear_nlink(ip);
  1231. discard_new_inode(ip);
  1232. } else {
  1233. d_instantiate_new(dentry, ip);
  1234. }
  1235. out1:
  1236. free_UCSname(&dname);
  1237. out:
  1238. jfs_info("jfs_mknod: returning %d", rc);
  1239. return rc;
  1240. }
  1241. static struct dentry *jfs_lookup(struct inode *dip, struct dentry *dentry, unsigned int flags)
  1242. {
  1243. struct btstack btstack;
  1244. ino_t inum;
  1245. struct inode *ip;
  1246. struct component_name key;
  1247. int rc;
  1248. jfs_info("jfs_lookup: name = %pd", dentry);
  1249. if ((rc = get_UCSname(&key, dentry)))
  1250. return ERR_PTR(rc);
  1251. rc = dtSearch(dip, &key, &inum, &btstack, JFS_LOOKUP);
  1252. free_UCSname(&key);
  1253. if (rc == -ENOENT) {
  1254. ip = NULL;
  1255. } else if (rc) {
  1256. jfs_err("jfs_lookup: dtSearch returned %d", rc);
  1257. ip = ERR_PTR(rc);
  1258. } else {
  1259. ip = jfs_iget(dip->i_sb, inum);
  1260. if (IS_ERR(ip))
  1261. jfs_err("jfs_lookup: iget failed on inum %d", (uint)inum);
  1262. }
  1263. return d_splice_alias(ip, dentry);
  1264. }
  1265. static struct inode *jfs_nfs_get_inode(struct super_block *sb,
  1266. u64 ino, u32 generation)
  1267. {
  1268. struct inode *inode;
  1269. if (ino == 0)
  1270. return ERR_PTR(-ESTALE);
  1271. inode = jfs_iget(sb, ino);
  1272. if (IS_ERR(inode))
  1273. return ERR_CAST(inode);
  1274. if (generation && inode->i_generation != generation) {
  1275. iput(inode);
  1276. return ERR_PTR(-ESTALE);
  1277. }
  1278. return inode;
  1279. }
  1280. struct dentry *jfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
  1281. int fh_len, int fh_type)
  1282. {
  1283. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  1284. jfs_nfs_get_inode);
  1285. }
  1286. struct dentry *jfs_fh_to_parent(struct super_block *sb, struct fid *fid,
  1287. int fh_len, int fh_type)
  1288. {
  1289. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  1290. jfs_nfs_get_inode);
  1291. }
  1292. struct dentry *jfs_get_parent(struct dentry *dentry)
  1293. {
  1294. unsigned long parent_ino;
  1295. parent_ino =
  1296. le32_to_cpu(JFS_IP(d_inode(dentry))->i_dtroot.header.idotdot);
  1297. return d_obtain_alias(jfs_iget(dentry->d_sb, parent_ino));
  1298. }
  1299. const struct inode_operations jfs_dir_inode_operations = {
  1300. .create = jfs_create,
  1301. .lookup = jfs_lookup,
  1302. .link = jfs_link,
  1303. .unlink = jfs_unlink,
  1304. .symlink = jfs_symlink,
  1305. .mkdir = jfs_mkdir,
  1306. .rmdir = jfs_rmdir,
  1307. .mknod = jfs_mknod,
  1308. .rename = jfs_rename,
  1309. .listxattr = jfs_listxattr,
  1310. .setattr = jfs_setattr,
  1311. #ifdef CONFIG_JFS_POSIX_ACL
  1312. .get_acl = jfs_get_acl,
  1313. .set_acl = jfs_set_acl,
  1314. #endif
  1315. };
  1316. const struct file_operations jfs_dir_operations = {
  1317. .read = generic_read_dir,
  1318. .iterate = jfs_readdir,
  1319. .fsync = jfs_fsync,
  1320. .unlocked_ioctl = jfs_ioctl,
  1321. #ifdef CONFIG_COMPAT
  1322. .compat_ioctl = jfs_compat_ioctl,
  1323. #endif
  1324. .llseek = generic_file_llseek,
  1325. };
  1326. static int jfs_ci_hash(const struct dentry *dir, struct qstr *this)
  1327. {
  1328. unsigned long hash;
  1329. int i;
  1330. hash = init_name_hash(dir);
  1331. for (i=0; i < this->len; i++)
  1332. hash = partial_name_hash(tolower(this->name[i]), hash);
  1333. this->hash = end_name_hash(hash);
  1334. return 0;
  1335. }
  1336. static int jfs_ci_compare(const struct dentry *dentry,
  1337. unsigned int len, const char *str, const struct qstr *name)
  1338. {
  1339. int i, result = 1;
  1340. if (len != name->len)
  1341. goto out;
  1342. for (i=0; i < len; i++) {
  1343. if (tolower(str[i]) != tolower(name->name[i]))
  1344. goto out;
  1345. }
  1346. result = 0;
  1347. out:
  1348. return result;
  1349. }
  1350. static int jfs_ci_revalidate(struct dentry *dentry, unsigned int flags)
  1351. {
  1352. /*
  1353. * This is not negative dentry. Always valid.
  1354. *
  1355. * Note, rename() to existing directory entry will have ->d_inode,
  1356. * and will use existing name which isn't specified name by user.
  1357. *
  1358. * We may be able to drop this positive dentry here. But dropping
  1359. * positive dentry isn't good idea. So it's unsupported like
  1360. * rename("filename", "FILENAME") for now.
  1361. */
  1362. if (d_really_is_positive(dentry))
  1363. return 1;
  1364. /*
  1365. * This may be nfsd (or something), anyway, we can't see the
  1366. * intent of this. So, since this can be for creation, drop it.
  1367. */
  1368. if (!flags)
  1369. return 0;
  1370. /*
  1371. * Drop the negative dentry, in order to make sure to use the
  1372. * case sensitive name which is specified by user if this is
  1373. * for creation.
  1374. */
  1375. if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
  1376. return 0;
  1377. return 1;
  1378. }
  1379. const struct dentry_operations jfs_ci_dentry_operations =
  1380. {
  1381. .d_hash = jfs_ci_hash,
  1382. .d_compare = jfs_ci_compare,
  1383. .d_revalidate = jfs_ci_revalidate,
  1384. };