namei.c 37 KB

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