namei.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574
  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 (IS_ERR(ip)) {
  92. rc = PTR_ERR(ip);
  93. goto out2;
  94. }
  95. tid = txBegin(dip->i_sb, 0);
  96. mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
  97. mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
  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. mutex_unlock(&JFS_IP(ip)->commit_mutex);
  145. mutex_unlock(&JFS_IP(dip)->commit_mutex);
  146. if (rc) {
  147. free_ea_wmap(ip);
  148. ip->i_nlink = 0;
  149. 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 (IS_ERR(ip)) {
  202. rc = PTR_ERR(ip);
  203. goto out2;
  204. }
  205. tid = txBegin(dip->i_sb, 0);
  206. mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
  207. mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
  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. inc_nlink(dip); /* 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. mutex_unlock(&JFS_IP(ip)->commit_mutex);
  257. mutex_unlock(&JFS_IP(dip)->commit_mutex);
  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. mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
  311. mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
  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. mutex_unlock(&JFS_IP(ip)->commit_mutex);
  327. mutex_unlock(&JFS_IP(dip)->commit_mutex);
  328. goto out2;
  329. }
  330. /* update parent directory's link count corresponding
  331. * to ".." entry of the target directory deleted
  332. */
  333. dip->i_ctime = dip->i_mtime = CURRENT_TIME;
  334. inode_dec_link_count(dip);
  335. /*
  336. * OS/2 could have created EA and/or ACL
  337. */
  338. /* free EA from both persistent and working map */
  339. if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
  340. /* free EA pages */
  341. txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
  342. }
  343. JFS_IP(ip)->ea.flag = 0;
  344. /* free ACL from both persistent and working map */
  345. if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
  346. /* free ACL pages */
  347. txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
  348. }
  349. JFS_IP(ip)->acl.flag = 0;
  350. /* mark the target directory as deleted */
  351. clear_nlink(ip);
  352. mark_inode_dirty(ip);
  353. rc = txCommit(tid, 2, &iplist[0], 0);
  354. txEnd(tid);
  355. mutex_unlock(&JFS_IP(ip)->commit_mutex);
  356. mutex_unlock(&JFS_IP(dip)->commit_mutex);
  357. /*
  358. * Truncating the directory index table is not guaranteed. It
  359. * may need to be done iteratively
  360. */
  361. if (test_cflag(COMMIT_Stale, dip)) {
  362. if (dip->i_size > 1)
  363. jfs_truncate_nolock(dip, 0);
  364. clear_cflag(COMMIT_Stale, dip);
  365. }
  366. out2:
  367. free_UCSname(&dname);
  368. out:
  369. jfs_info("jfs_rmdir: rc:%d", rc);
  370. return rc;
  371. }
  372. /*
  373. * NAME: jfs_unlink(dip, dentry)
  374. *
  375. * FUNCTION: remove a link to object <vp> named by <name>
  376. * from parent directory <dvp>
  377. *
  378. * PARAMETER: dip - inode of parent directory
  379. * dentry - dentry of object to be removed
  380. *
  381. * RETURN: errors from subroutines
  382. *
  383. * note:
  384. * temporary file: if one or more processes have the file open
  385. * when the last link is removed, the link will be removed before
  386. * unlink() returns, but the removal of the file contents will be
  387. * postponed until all references to the files are closed.
  388. *
  389. * JFS does NOT support unlink() on directories.
  390. *
  391. */
  392. static int jfs_unlink(struct inode *dip, struct dentry *dentry)
  393. {
  394. int rc;
  395. tid_t tid; /* transaction id */
  396. struct inode *ip = dentry->d_inode;
  397. ino_t ino;
  398. struct component_name dname; /* object name */
  399. struct inode *iplist[2];
  400. struct tblock *tblk;
  401. s64 new_size = 0;
  402. int commit_flag;
  403. jfs_info("jfs_unlink: dip:0x%p name:%s", dip, dentry->d_name.name);
  404. /* Init inode for quota operations. */
  405. DQUOT_INIT(ip);
  406. if ((rc = get_UCSname(&dname, dentry)))
  407. goto out;
  408. IWRITE_LOCK(ip, RDWRLOCK_NORMAL);
  409. tid = txBegin(dip->i_sb, 0);
  410. mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
  411. mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
  412. iplist[0] = dip;
  413. iplist[1] = ip;
  414. /*
  415. * delete the entry of target file from parent directory
  416. */
  417. ino = ip->i_ino;
  418. if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
  419. jfs_err("jfs_unlink: dtDelete returned %d", rc);
  420. if (rc == -EIO)
  421. txAbort(tid, 1); /* Marks FS Dirty */
  422. txEnd(tid);
  423. mutex_unlock(&JFS_IP(ip)->commit_mutex);
  424. mutex_unlock(&JFS_IP(dip)->commit_mutex);
  425. IWRITE_UNLOCK(ip);
  426. goto out1;
  427. }
  428. ASSERT(ip->i_nlink);
  429. ip->i_ctime = dip->i_ctime = dip->i_mtime = CURRENT_TIME;
  430. mark_inode_dirty(dip);
  431. /* update target's inode */
  432. inode_dec_link_count(ip);
  433. /*
  434. * commit zero link count object
  435. */
  436. if (ip->i_nlink == 0) {
  437. assert(!test_cflag(COMMIT_Nolink, ip));
  438. /* free block resources */
  439. if ((new_size = commitZeroLink(tid, ip)) < 0) {
  440. txAbort(tid, 1); /* Marks FS Dirty */
  441. txEnd(tid);
  442. mutex_unlock(&JFS_IP(ip)->commit_mutex);
  443. mutex_unlock(&JFS_IP(dip)->commit_mutex);
  444. IWRITE_UNLOCK(ip);
  445. rc = new_size;
  446. goto out1;
  447. }
  448. tblk = tid_to_tblock(tid);
  449. tblk->xflag |= COMMIT_DELETE;
  450. tblk->u.ip = ip;
  451. }
  452. /*
  453. * Incomplete truncate of file data can
  454. * result in timing problems unless we synchronously commit the
  455. * transaction.
  456. */
  457. if (new_size)
  458. commit_flag = COMMIT_SYNC;
  459. else
  460. commit_flag = 0;
  461. /*
  462. * If xtTruncate was incomplete, commit synchronously to avoid
  463. * timing complications
  464. */
  465. rc = txCommit(tid, 2, &iplist[0], commit_flag);
  466. txEnd(tid);
  467. mutex_unlock(&JFS_IP(ip)->commit_mutex);
  468. mutex_unlock(&JFS_IP(dip)->commit_mutex);
  469. while (new_size && (rc == 0)) {
  470. tid = txBegin(dip->i_sb, 0);
  471. mutex_lock(&JFS_IP(ip)->commit_mutex);
  472. new_size = xtTruncate_pmap(tid, ip, new_size);
  473. if (new_size < 0) {
  474. txAbort(tid, 1); /* Marks FS Dirty */
  475. rc = new_size;
  476. } else
  477. rc = txCommit(tid, 2, &iplist[0], COMMIT_SYNC);
  478. txEnd(tid);
  479. mutex_unlock(&JFS_IP(ip)->commit_mutex);
  480. }
  481. if (ip->i_nlink == 0)
  482. set_cflag(COMMIT_Nolink, ip);
  483. IWRITE_UNLOCK(ip);
  484. /*
  485. * Truncating the directory index table is not guaranteed. It
  486. * may need to be done iteratively
  487. */
  488. if (test_cflag(COMMIT_Stale, dip)) {
  489. if (dip->i_size > 1)
  490. jfs_truncate_nolock(dip, 0);
  491. clear_cflag(COMMIT_Stale, dip);
  492. }
  493. out1:
  494. free_UCSname(&dname);
  495. out:
  496. jfs_info("jfs_unlink: rc:%d", rc);
  497. return rc;
  498. }
  499. /*
  500. * NAME: commitZeroLink()
  501. *
  502. * FUNCTION: for non-directory, called by jfs_remove(),
  503. * truncate a regular file, directory or symbolic
  504. * link to zero length. return 0 if type is not
  505. * one of these.
  506. *
  507. * if the file is currently associated with a VM segment
  508. * only permanent disk and inode map resources are freed,
  509. * and neither the inode nor indirect blocks are modified
  510. * so that the resources can be later freed in the work
  511. * map by ctrunc1.
  512. * if there is no VM segment on entry, the resources are
  513. * freed in both work and permanent map.
  514. * (? for temporary file - memory object is cached even
  515. * after no reference:
  516. * reference count > 0 - )
  517. *
  518. * PARAMETERS: cd - pointer to commit data structure.
  519. * current inode is the one to truncate.
  520. *
  521. * RETURN: Errors from subroutines
  522. */
  523. static s64 commitZeroLink(tid_t tid, struct inode *ip)
  524. {
  525. int filetype;
  526. struct tblock *tblk;
  527. jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid, ip);
  528. filetype = ip->i_mode & S_IFMT;
  529. switch (filetype) {
  530. case S_IFREG:
  531. break;
  532. case S_IFLNK:
  533. /* fast symbolic link */
  534. if (ip->i_size < IDATASIZE) {
  535. ip->i_size = 0;
  536. return 0;
  537. }
  538. break;
  539. default:
  540. assert(filetype != S_IFDIR);
  541. return 0;
  542. }
  543. set_cflag(COMMIT_Freewmap, ip);
  544. /* mark transaction of block map update type */
  545. tblk = tid_to_tblock(tid);
  546. tblk->xflag |= COMMIT_PMAP;
  547. /*
  548. * free EA
  549. */
  550. if (JFS_IP(ip)->ea.flag & DXD_EXTENT)
  551. /* acquire maplock on EA to be freed from block map */
  552. txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
  553. /*
  554. * free ACL
  555. */
  556. if (JFS_IP(ip)->acl.flag & DXD_EXTENT)
  557. /* acquire maplock on EA to be freed from block map */
  558. txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
  559. /*
  560. * free xtree/data (truncate to zero length):
  561. * free xtree/data pages from cache if COMMIT_PWMAP,
  562. * free xtree/data blocks from persistent block map, and
  563. * free xtree/data blocks from working block map if COMMIT_PWMAP;
  564. */
  565. if (ip->i_size)
  566. return xtTruncate_pmap(tid, ip, 0);
  567. return 0;
  568. }
  569. /*
  570. * NAME: jfs_free_zero_link()
  571. *
  572. * FUNCTION: for non-directory, called by iClose(),
  573. * free resources of a file from cache and WORKING map
  574. * for a file previously committed with zero link count
  575. * while associated with a pager object,
  576. *
  577. * PARAMETER: ip - pointer to inode of file.
  578. */
  579. void jfs_free_zero_link(struct inode *ip)
  580. {
  581. int type;
  582. jfs_info("jfs_free_zero_link: ip = 0x%p", ip);
  583. /* return if not reg or symbolic link or if size is
  584. * already ok.
  585. */
  586. type = ip->i_mode & S_IFMT;
  587. switch (type) {
  588. case S_IFREG:
  589. break;
  590. case S_IFLNK:
  591. /* if its contained in inode nothing to do */
  592. if (ip->i_size < IDATASIZE)
  593. return;
  594. break;
  595. default:
  596. return;
  597. }
  598. /*
  599. * free EA
  600. */
  601. if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
  602. s64 xaddr = addressDXD(&JFS_IP(ip)->ea);
  603. int xlen = lengthDXD(&JFS_IP(ip)->ea);
  604. struct maplock maplock; /* maplock for COMMIT_WMAP */
  605. struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
  606. /* free EA pages from cache */
  607. invalidate_dxd_metapages(ip, JFS_IP(ip)->ea);
  608. /* free EA extent from working block map */
  609. maplock.index = 1;
  610. pxdlock = (struct pxd_lock *) & maplock;
  611. pxdlock->flag = mlckFREEPXD;
  612. PXDaddress(&pxdlock->pxd, xaddr);
  613. PXDlength(&pxdlock->pxd, xlen);
  614. txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
  615. }
  616. /*
  617. * free ACL
  618. */
  619. if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
  620. s64 xaddr = addressDXD(&JFS_IP(ip)->acl);
  621. int xlen = lengthDXD(&JFS_IP(ip)->acl);
  622. struct maplock maplock; /* maplock for COMMIT_WMAP */
  623. struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
  624. invalidate_dxd_metapages(ip, JFS_IP(ip)->acl);
  625. /* free ACL extent from working block map */
  626. maplock.index = 1;
  627. pxdlock = (struct pxd_lock *) & maplock;
  628. pxdlock->flag = mlckFREEPXD;
  629. PXDaddress(&pxdlock->pxd, xaddr);
  630. PXDlength(&pxdlock->pxd, xlen);
  631. txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
  632. }
  633. /*
  634. * free xtree/data (truncate to zero length):
  635. * free xtree/data pages from cache, and
  636. * free xtree/data blocks from working block map;
  637. */
  638. if (ip->i_size)
  639. xtTruncate(0, ip, 0, COMMIT_WMAP);
  640. }
  641. /*
  642. * NAME: jfs_link(vp, dvp, name, crp)
  643. *
  644. * FUNCTION: create a link to <vp> by the name = <name>
  645. * in the parent directory <dvp>
  646. *
  647. * PARAMETER: vp - target object
  648. * dvp - parent directory of new link
  649. * name - name of new link to target object
  650. * crp - credential
  651. *
  652. * RETURN: Errors from subroutines
  653. *
  654. * note:
  655. * JFS does NOT support link() on directories (to prevent circular
  656. * path in the directory hierarchy);
  657. * EPERM: the target object is a directory, and either the caller
  658. * does not have appropriate privileges or the implementation prohibits
  659. * using link() on directories [XPG4.2].
  660. *
  661. * JFS does NOT support links between file systems:
  662. * EXDEV: target object and new link are on different file systems and
  663. * implementation does not support links between file systems [XPG4.2].
  664. */
  665. static int jfs_link(struct dentry *old_dentry,
  666. struct inode *dir, struct dentry *dentry)
  667. {
  668. int rc;
  669. tid_t tid;
  670. struct inode *ip = old_dentry->d_inode;
  671. ino_t ino;
  672. struct component_name dname;
  673. struct btstack btstack;
  674. struct inode *iplist[2];
  675. jfs_info("jfs_link: %s %s", old_dentry->d_name.name,
  676. dentry->d_name.name);
  677. if (ip->i_nlink == JFS_LINK_MAX)
  678. return -EMLINK;
  679. if (ip->i_nlink == 0)
  680. return -ENOENT;
  681. tid = txBegin(ip->i_sb, 0);
  682. mutex_lock_nested(&JFS_IP(dir)->commit_mutex, COMMIT_MUTEX_PARENT);
  683. mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
  684. /*
  685. * scan parent directory for entry/freespace
  686. */
  687. if ((rc = get_UCSname(&dname, dentry)))
  688. goto out;
  689. if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
  690. goto free_dname;
  691. /*
  692. * create entry for new link in parent directory
  693. */
  694. ino = ip->i_ino;
  695. if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
  696. goto free_dname;
  697. /* update object inode */
  698. inc_nlink(ip); /* for new link */
  699. ip->i_ctime = CURRENT_TIME;
  700. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  701. mark_inode_dirty(dir);
  702. atomic_inc(&ip->i_count);
  703. iplist[0] = ip;
  704. iplist[1] = dir;
  705. rc = txCommit(tid, 2, &iplist[0], 0);
  706. if (rc) {
  707. ip->i_nlink--; /* never instantiated */
  708. iput(ip);
  709. } else
  710. d_instantiate(dentry, ip);
  711. free_dname:
  712. free_UCSname(&dname);
  713. out:
  714. txEnd(tid);
  715. mutex_unlock(&JFS_IP(ip)->commit_mutex);
  716. mutex_unlock(&JFS_IP(dir)->commit_mutex);
  717. jfs_info("jfs_link: rc:%d", rc);
  718. return rc;
  719. }
  720. /*
  721. * NAME: jfs_symlink(dip, dentry, name)
  722. *
  723. * FUNCTION: creates a symbolic link to <symlink> by name <name>
  724. * in directory <dip>
  725. *
  726. * PARAMETER: dip - parent directory vnode
  727. * dentry - dentry of symbolic link
  728. * name - the path name of the existing object
  729. * that will be the source of the link
  730. *
  731. * RETURN: errors from subroutines
  732. *
  733. * note:
  734. * ENAMETOOLONG: pathname resolution of a symbolic link produced
  735. * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
  736. */
  737. static int jfs_symlink(struct inode *dip, struct dentry *dentry,
  738. const char *name)
  739. {
  740. int rc;
  741. tid_t tid;
  742. ino_t ino = 0;
  743. struct component_name dname;
  744. int ssize; /* source pathname size */
  745. struct btstack btstack;
  746. struct inode *ip = dentry->d_inode;
  747. unchar *i_fastsymlink;
  748. s64 xlen = 0;
  749. int bmask = 0, xsize;
  750. s64 extent = 0, xaddr;
  751. struct metapage *mp;
  752. struct super_block *sb;
  753. struct tblock *tblk;
  754. struct inode *iplist[2];
  755. jfs_info("jfs_symlink: dip:0x%p name:%s", dip, name);
  756. ssize = strlen(name) + 1;
  757. /*
  758. * search parent directory for entry/freespace
  759. * (dtSearch() returns parent directory page pinned)
  760. */
  761. if ((rc = get_UCSname(&dname, dentry)))
  762. goto out1;
  763. /*
  764. * allocate on-disk/in-memory inode for symbolic link:
  765. * (iAlloc() returns new, locked inode)
  766. */
  767. ip = ialloc(dip, S_IFLNK | 0777);
  768. if (IS_ERR(ip)) {
  769. rc = PTR_ERR(ip);
  770. goto out2;
  771. }
  772. tid = txBegin(dip->i_sb, 0);
  773. mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
  774. mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
  775. rc = jfs_init_security(tid, ip, dip);
  776. if (rc)
  777. goto out3;
  778. tblk = tid_to_tblock(tid);
  779. tblk->xflag |= COMMIT_CREATE;
  780. tblk->ino = ip->i_ino;
  781. tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
  782. /* fix symlink access permission
  783. * (dir_create() ANDs in the u.u_cmask,
  784. * but symlinks really need to be 777 access)
  785. */
  786. ip->i_mode |= 0777;
  787. /*
  788. * write symbolic link target path name
  789. */
  790. xtInitRoot(tid, ip);
  791. /*
  792. * write source path name inline in on-disk inode (fast symbolic link)
  793. */
  794. if (ssize <= IDATASIZE) {
  795. ip->i_op = &jfs_symlink_inode_operations;
  796. i_fastsymlink = JFS_IP(ip)->i_inline;
  797. memcpy(i_fastsymlink, name, ssize);
  798. ip->i_size = ssize - 1;
  799. /*
  800. * if symlink is > 128 bytes, we don't have the space to
  801. * store inline extended attributes
  802. */
  803. if (ssize > sizeof (JFS_IP(ip)->i_inline))
  804. JFS_IP(ip)->mode2 &= ~INLINEEA;
  805. jfs_info("jfs_symlink: fast symlink added ssize:%d name:%s ",
  806. ssize, name);
  807. }
  808. /*
  809. * write source path name in a single extent
  810. */
  811. else {
  812. jfs_info("jfs_symlink: allocate extent ip:0x%p", ip);
  813. ip->i_op = &page_symlink_inode_operations;
  814. ip->i_mapping->a_ops = &jfs_aops;
  815. /*
  816. * even though the data of symlink object (source
  817. * path name) is treated as non-journaled user data,
  818. * it is read/written thru buffer cache for performance.
  819. */
  820. sb = ip->i_sb;
  821. bmask = JFS_SBI(sb)->bsize - 1;
  822. xsize = (ssize + bmask) & ~bmask;
  823. xaddr = 0;
  824. xlen = xsize >> JFS_SBI(sb)->l2bsize;
  825. if ((rc = xtInsert(tid, ip, 0, 0, xlen, &xaddr, 0))) {
  826. txAbort(tid, 0);
  827. goto out3;
  828. }
  829. extent = xaddr;
  830. ip->i_size = ssize - 1;
  831. while (ssize) {
  832. /* This is kind of silly since PATH_MAX == 4K */
  833. int copy_size = min(ssize, PSIZE);
  834. mp = get_metapage(ip, xaddr, PSIZE, 1);
  835. if (mp == NULL) {
  836. xtTruncate(tid, ip, 0, COMMIT_PWMAP);
  837. rc = -EIO;
  838. txAbort(tid, 0);
  839. goto out3;
  840. }
  841. memcpy(mp->data, name, copy_size);
  842. flush_metapage(mp);
  843. ssize -= copy_size;
  844. name += copy_size;
  845. xaddr += JFS_SBI(sb)->nbperpage;
  846. }
  847. }
  848. /*
  849. * create entry for symbolic link in parent directory
  850. */
  851. rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE);
  852. if (rc == 0) {
  853. ino = ip->i_ino;
  854. rc = dtInsert(tid, dip, &dname, &ino, &btstack);
  855. }
  856. if (rc) {
  857. if (xlen)
  858. xtTruncate(tid, ip, 0, COMMIT_PWMAP);
  859. txAbort(tid, 0);
  860. /* discard new inode */
  861. goto out3;
  862. }
  863. insert_inode_hash(ip);
  864. mark_inode_dirty(ip);
  865. dip->i_ctime = dip->i_mtime = CURRENT_TIME;
  866. mark_inode_dirty(dip);
  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. mutex_unlock(&JFS_IP(ip)->commit_mutex);
  876. mutex_unlock(&JFS_IP(dip)->commit_mutex);
  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, RDWRLOCK_NORMAL);
  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. /*
  966. * How do we know the locking is safe from deadlocks?
  967. * The vfs does the hard part for us. Any time we are taking nested
  968. * commit_mutexes, the vfs already has i_mutex held on the parent.
  969. * Here, the vfs has already taken i_mutex on both old_dir and new_dir.
  970. */
  971. mutex_lock_nested(&JFS_IP(new_dir)->commit_mutex, COMMIT_MUTEX_PARENT);
  972. mutex_lock_nested(&JFS_IP(old_ip)->commit_mutex, COMMIT_MUTEX_CHILD);
  973. if (old_dir != new_dir)
  974. mutex_lock_nested(&JFS_IP(old_dir)->commit_mutex,
  975. COMMIT_MUTEX_SECOND_PARENT);
  976. if (new_ip) {
  977. mutex_lock_nested(&JFS_IP(new_ip)->commit_mutex,
  978. COMMIT_MUTEX_VICTIM);
  979. /*
  980. * Change existing directory entry to new inode number
  981. */
  982. ino = new_ip->i_ino;
  983. rc = dtModify(tid, new_dir, &new_dname, &ino,
  984. old_ip->i_ino, JFS_RENAME);
  985. if (rc)
  986. goto out4;
  987. drop_nlink(new_ip);
  988. if (S_ISDIR(new_ip->i_mode)) {
  989. drop_nlink(new_ip);
  990. if (new_ip->i_nlink) {
  991. mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
  992. if (old_dir != new_dir)
  993. mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
  994. mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
  995. mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
  996. if (!S_ISDIR(old_ip->i_mode) && new_ip)
  997. IWRITE_UNLOCK(new_ip);
  998. jfs_error(new_ip->i_sb,
  999. "jfs_rename: new_ip->i_nlink != 0");
  1000. return -EIO;
  1001. }
  1002. tblk = tid_to_tblock(tid);
  1003. tblk->xflag |= COMMIT_DELETE;
  1004. tblk->u.ip = new_ip;
  1005. } else if (new_ip->i_nlink == 0) {
  1006. assert(!test_cflag(COMMIT_Nolink, new_ip));
  1007. /* free block resources */
  1008. if ((new_size = commitZeroLink(tid, new_ip)) < 0) {
  1009. txAbort(tid, 1); /* Marks FS Dirty */
  1010. rc = new_size;
  1011. goto out4;
  1012. }
  1013. tblk = tid_to_tblock(tid);
  1014. tblk->xflag |= COMMIT_DELETE;
  1015. tblk->u.ip = new_ip;
  1016. } else {
  1017. new_ip->i_ctime = CURRENT_TIME;
  1018. mark_inode_dirty(new_ip);
  1019. }
  1020. } else {
  1021. /*
  1022. * Add new directory entry
  1023. */
  1024. rc = dtSearch(new_dir, &new_dname, &ino, &btstack,
  1025. JFS_CREATE);
  1026. if (rc) {
  1027. jfs_err("jfs_rename didn't expect dtSearch to fail "
  1028. "w/rc = %d", rc);
  1029. goto out4;
  1030. }
  1031. ino = old_ip->i_ino;
  1032. rc = dtInsert(tid, new_dir, &new_dname, &ino, &btstack);
  1033. if (rc) {
  1034. if (rc == -EIO)
  1035. jfs_err("jfs_rename: dtInsert returned -EIO");
  1036. goto out4;
  1037. }
  1038. if (S_ISDIR(old_ip->i_mode))
  1039. inc_nlink(new_dir);
  1040. }
  1041. /*
  1042. * Remove old directory entry
  1043. */
  1044. ino = old_ip->i_ino;
  1045. rc = dtDelete(tid, old_dir, &old_dname, &ino, JFS_REMOVE);
  1046. if (rc) {
  1047. jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
  1048. rc);
  1049. txAbort(tid, 1); /* Marks Filesystem dirty */
  1050. goto out4;
  1051. }
  1052. if (S_ISDIR(old_ip->i_mode)) {
  1053. drop_nlink(old_dir);
  1054. if (old_dir != new_dir) {
  1055. /*
  1056. * Change inode number of parent for moved directory
  1057. */
  1058. JFS_IP(old_ip)->i_dtroot.header.idotdot =
  1059. cpu_to_le32(new_dir->i_ino);
  1060. /* Linelock header of dtree */
  1061. tlck = txLock(tid, old_ip,
  1062. (struct metapage *) &JFS_IP(old_ip)->bxflag,
  1063. tlckDTREE | tlckBTROOT | tlckRELINK);
  1064. dtlck = (struct dt_lock *) & tlck->lock;
  1065. ASSERT(dtlck->index == 0);
  1066. lv = & dtlck->lv[0];
  1067. lv->offset = 0;
  1068. lv->length = 1;
  1069. dtlck->index++;
  1070. }
  1071. }
  1072. /*
  1073. * Update ctime on changed/moved inodes & mark dirty
  1074. */
  1075. old_ip->i_ctime = CURRENT_TIME;
  1076. mark_inode_dirty(old_ip);
  1077. new_dir->i_ctime = new_dir->i_mtime = current_fs_time(new_dir->i_sb);
  1078. mark_inode_dirty(new_dir);
  1079. /* Build list of inodes modified by this transaction */
  1080. ipcount = 0;
  1081. iplist[ipcount++] = old_ip;
  1082. if (new_ip)
  1083. iplist[ipcount++] = new_ip;
  1084. iplist[ipcount++] = old_dir;
  1085. if (old_dir != new_dir) {
  1086. iplist[ipcount++] = new_dir;
  1087. old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
  1088. mark_inode_dirty(old_dir);
  1089. }
  1090. /*
  1091. * Incomplete truncate of file data can
  1092. * result in timing problems unless we synchronously commit the
  1093. * transaction.
  1094. */
  1095. if (new_size)
  1096. commit_flag = COMMIT_SYNC;
  1097. else
  1098. commit_flag = 0;
  1099. rc = txCommit(tid, ipcount, iplist, commit_flag);
  1100. out4:
  1101. txEnd(tid);
  1102. if (new_ip)
  1103. mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
  1104. if (old_dir != new_dir)
  1105. mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
  1106. mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
  1107. mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
  1108. while (new_size && (rc == 0)) {
  1109. tid = txBegin(new_ip->i_sb, 0);
  1110. mutex_lock(&JFS_IP(new_ip)->commit_mutex);
  1111. new_size = xtTruncate_pmap(tid, new_ip, new_size);
  1112. if (new_size < 0) {
  1113. txAbort(tid, 1);
  1114. rc = new_size;
  1115. } else
  1116. rc = txCommit(tid, 1, &new_ip, COMMIT_SYNC);
  1117. txEnd(tid);
  1118. mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
  1119. }
  1120. if (new_ip && (new_ip->i_nlink == 0))
  1121. set_cflag(COMMIT_Nolink, new_ip);
  1122. out3:
  1123. free_UCSname(&new_dname);
  1124. out2:
  1125. free_UCSname(&old_dname);
  1126. out1:
  1127. if (new_ip && !S_ISDIR(new_ip->i_mode))
  1128. IWRITE_UNLOCK(new_ip);
  1129. /*
  1130. * Truncating the directory index table is not guaranteed. It
  1131. * may need to be done iteratively
  1132. */
  1133. if (test_cflag(COMMIT_Stale, old_dir)) {
  1134. if (old_dir->i_size > 1)
  1135. jfs_truncate_nolock(old_dir, 0);
  1136. clear_cflag(COMMIT_Stale, old_dir);
  1137. }
  1138. jfs_info("jfs_rename: returning %d", rc);
  1139. return rc;
  1140. }
  1141. /*
  1142. * NAME: jfs_mknod
  1143. *
  1144. * FUNCTION: Create a special file (device)
  1145. */
  1146. static int jfs_mknod(struct inode *dir, struct dentry *dentry,
  1147. int mode, dev_t rdev)
  1148. {
  1149. struct jfs_inode_info *jfs_ip;
  1150. struct btstack btstack;
  1151. struct component_name dname;
  1152. ino_t ino;
  1153. struct inode *ip;
  1154. struct inode *iplist[2];
  1155. int rc;
  1156. tid_t tid;
  1157. struct tblock *tblk;
  1158. if (!new_valid_dev(rdev))
  1159. return -EINVAL;
  1160. jfs_info("jfs_mknod: %s", dentry->d_name.name);
  1161. if ((rc = get_UCSname(&dname, dentry)))
  1162. goto out;
  1163. ip = ialloc(dir, mode);
  1164. if (IS_ERR(ip)) {
  1165. rc = PTR_ERR(ip);
  1166. goto out1;
  1167. }
  1168. jfs_ip = JFS_IP(ip);
  1169. tid = txBegin(dir->i_sb, 0);
  1170. mutex_lock_nested(&JFS_IP(dir)->commit_mutex, COMMIT_MUTEX_PARENT);
  1171. mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
  1172. rc = jfs_init_acl(tid, ip, dir);
  1173. if (rc)
  1174. goto out3;
  1175. rc = jfs_init_security(tid, ip, dir);
  1176. if (rc) {
  1177. txAbort(tid, 0);
  1178. goto out3;
  1179. }
  1180. if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE))) {
  1181. txAbort(tid, 0);
  1182. goto out3;
  1183. }
  1184. tblk = tid_to_tblock(tid);
  1185. tblk->xflag |= COMMIT_CREATE;
  1186. tblk->ino = ip->i_ino;
  1187. tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
  1188. ino = ip->i_ino;
  1189. if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack))) {
  1190. txAbort(tid, 0);
  1191. goto out3;
  1192. }
  1193. ip->i_op = &jfs_file_inode_operations;
  1194. jfs_ip->dev = new_encode_dev(rdev);
  1195. init_special_inode(ip, ip->i_mode, rdev);
  1196. insert_inode_hash(ip);
  1197. mark_inode_dirty(ip);
  1198. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1199. mark_inode_dirty(dir);
  1200. iplist[0] = dir;
  1201. iplist[1] = ip;
  1202. rc = txCommit(tid, 2, iplist, 0);
  1203. out3:
  1204. txEnd(tid);
  1205. mutex_unlock(&JFS_IP(ip)->commit_mutex);
  1206. mutex_unlock(&JFS_IP(dir)->commit_mutex);
  1207. if (rc) {
  1208. free_ea_wmap(ip);
  1209. ip->i_nlink = 0;
  1210. iput(ip);
  1211. } else
  1212. d_instantiate(dentry, ip);
  1213. out1:
  1214. free_UCSname(&dname);
  1215. out:
  1216. jfs_info("jfs_mknod: returning %d", rc);
  1217. return rc;
  1218. }
  1219. static struct dentry *jfs_lookup(struct inode *dip, struct dentry *dentry, struct nameidata *nd)
  1220. {
  1221. struct btstack btstack;
  1222. ino_t inum;
  1223. struct inode *ip;
  1224. struct component_name key;
  1225. const char *name = dentry->d_name.name;
  1226. int len = dentry->d_name.len;
  1227. int rc;
  1228. jfs_info("jfs_lookup: name = %s", name);
  1229. if (JFS_SBI(dip->i_sb)->mntflag & JFS_OS2)
  1230. dentry->d_op = &jfs_ci_dentry_operations;
  1231. if ((name[0] == '.') && (len == 1))
  1232. inum = dip->i_ino;
  1233. else if (strcmp(name, "..") == 0)
  1234. inum = PARENT(dip);
  1235. else {
  1236. if ((rc = get_UCSname(&key, dentry)))
  1237. return ERR_PTR(rc);
  1238. rc = dtSearch(dip, &key, &inum, &btstack, JFS_LOOKUP);
  1239. free_UCSname(&key);
  1240. if (rc == -ENOENT) {
  1241. d_add(dentry, NULL);
  1242. return ERR_PTR(0);
  1243. } else if (rc) {
  1244. jfs_err("jfs_lookup: dtSearch returned %d", rc);
  1245. return ERR_PTR(rc);
  1246. }
  1247. }
  1248. ip = iget(dip->i_sb, inum);
  1249. if (ip == NULL || is_bad_inode(ip)) {
  1250. jfs_err("jfs_lookup: iget failed on inum %d", (uint) inum);
  1251. if (ip)
  1252. iput(ip);
  1253. return ERR_PTR(-EACCES);
  1254. }
  1255. dentry = d_splice_alias(ip, dentry);
  1256. if (dentry && (JFS_SBI(dip->i_sb)->mntflag & JFS_OS2))
  1257. dentry->d_op = &jfs_ci_dentry_operations;
  1258. return dentry;
  1259. }
  1260. struct dentry *jfs_get_parent(struct dentry *dentry)
  1261. {
  1262. struct super_block *sb = dentry->d_inode->i_sb;
  1263. struct dentry *parent = ERR_PTR(-ENOENT);
  1264. struct inode *inode;
  1265. unsigned long parent_ino;
  1266. parent_ino =
  1267. le32_to_cpu(JFS_IP(dentry->d_inode)->i_dtroot.header.idotdot);
  1268. inode = iget(sb, parent_ino);
  1269. if (inode) {
  1270. if (is_bad_inode(inode)) {
  1271. iput(inode);
  1272. parent = ERR_PTR(-EACCES);
  1273. } else {
  1274. parent = d_alloc_anon(inode);
  1275. if (!parent) {
  1276. parent = ERR_PTR(-ENOMEM);
  1277. iput(inode);
  1278. }
  1279. }
  1280. }
  1281. return parent;
  1282. }
  1283. const struct inode_operations jfs_dir_inode_operations = {
  1284. .create = jfs_create,
  1285. .lookup = jfs_lookup,
  1286. .link = jfs_link,
  1287. .unlink = jfs_unlink,
  1288. .symlink = jfs_symlink,
  1289. .mkdir = jfs_mkdir,
  1290. .rmdir = jfs_rmdir,
  1291. .mknod = jfs_mknod,
  1292. .rename = jfs_rename,
  1293. .setxattr = jfs_setxattr,
  1294. .getxattr = jfs_getxattr,
  1295. .listxattr = jfs_listxattr,
  1296. .removexattr = jfs_removexattr,
  1297. #ifdef CONFIG_JFS_POSIX_ACL
  1298. .setattr = jfs_setattr,
  1299. .permission = jfs_permission,
  1300. #endif
  1301. };
  1302. const struct file_operations jfs_dir_operations = {
  1303. .read = generic_read_dir,
  1304. .readdir = jfs_readdir,
  1305. .fsync = jfs_fsync,
  1306. .ioctl = jfs_ioctl,
  1307. };
  1308. static int jfs_ci_hash(struct dentry *dir, struct qstr *this)
  1309. {
  1310. unsigned long hash;
  1311. int i;
  1312. hash = init_name_hash();
  1313. for (i=0; i < this->len; i++)
  1314. hash = partial_name_hash(tolower(this->name[i]), hash);
  1315. this->hash = end_name_hash(hash);
  1316. return 0;
  1317. }
  1318. static int jfs_ci_compare(struct dentry *dir, struct qstr *a, struct qstr *b)
  1319. {
  1320. int i, result = 1;
  1321. if (a->len != b->len)
  1322. goto out;
  1323. for (i=0; i < a->len; i++) {
  1324. if (tolower(a->name[i]) != tolower(b->name[i]))
  1325. goto out;
  1326. }
  1327. result = 0;
  1328. /*
  1329. * We want creates to preserve case. A negative dentry, a, that
  1330. * has a different case than b may cause a new entry to be created
  1331. * with the wrong case. Since we can't tell if a comes from a negative
  1332. * dentry, we blindly replace it with b. This should be harmless if
  1333. * a is not a negative dentry.
  1334. */
  1335. memcpy((unsigned char *)a->name, b->name, a->len);
  1336. out:
  1337. return result;
  1338. }
  1339. struct dentry_operations jfs_ci_dentry_operations =
  1340. {
  1341. .d_hash = jfs_ci_hash,
  1342. .d_compare = jfs_ci_compare,
  1343. };