namei.c 35 KB

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