namei.c 38 KB

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