namei.c 37 KB

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