namei.c 37 KB

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