namei.c 38 KB

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