namei.c 37 KB

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