namei.c 34 KB

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