namei.c 36 KB

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