namei.c 37 KB

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