namei.c 37 KB

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