namei.c 37 KB

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