namei.c 37 KB

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