namei.c 37 KB

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