namei.c 35 KB

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