namei.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * namei.c
  5. *
  6. * Create and rename file, directory, symlinks
  7. *
  8. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  9. *
  10. * Portions of this code from linux/fs/ext3/dir.c
  11. *
  12. * Copyright (C) 1992, 1993, 1994, 1995
  13. * Remy Card (card@masi.ibp.fr)
  14. * Laboratoire MASI - Institut Blaise pascal
  15. * Universite Pierre et Marie Curie (Paris VI)
  16. *
  17. * from
  18. *
  19. * linux/fs/minix/dir.c
  20. *
  21. * Copyright (C) 1991, 1992 Linux Torvalds
  22. *
  23. * This program is free software; you can redistribute it and/or
  24. * modify it under the terms of the GNU General Public
  25. * License as published by the Free Software Foundation; either
  26. * version 2 of the License, or (at your option) any later version.
  27. *
  28. * This program is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  31. * General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU General Public
  34. * License along with this program; if not, write to the
  35. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  36. * Boston, MA 021110-1307, USA.
  37. */
  38. #include <linux/fs.h>
  39. #include <linux/types.h>
  40. #include <linux/slab.h>
  41. #include <linux/highmem.h>
  42. #define MLOG_MASK_PREFIX ML_NAMEI
  43. #include <cluster/masklog.h>
  44. #include "ocfs2.h"
  45. #include "alloc.h"
  46. #include "dcache.h"
  47. #include "dir.h"
  48. #include "dlmglue.h"
  49. #include "extent_map.h"
  50. #include "file.h"
  51. #include "inode.h"
  52. #include "journal.h"
  53. #include "namei.h"
  54. #include "suballoc.h"
  55. #include "super.h"
  56. #include "symlink.h"
  57. #include "sysfile.h"
  58. #include "uptodate.h"
  59. #include "xattr.h"
  60. #include "acl.h"
  61. #include "buffer_head_io.h"
  62. static int ocfs2_mknod_locked(struct ocfs2_super *osb,
  63. struct inode *dir,
  64. struct inode *inode,
  65. struct dentry *dentry,
  66. dev_t dev,
  67. struct buffer_head **new_fe_bh,
  68. struct buffer_head *parent_fe_bh,
  69. handle_t *handle,
  70. struct ocfs2_alloc_context *inode_ac);
  71. static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
  72. struct inode **ret_orphan_dir,
  73. struct inode *inode,
  74. char *name,
  75. struct buffer_head **de_bh);
  76. static int ocfs2_orphan_add(struct ocfs2_super *osb,
  77. handle_t *handle,
  78. struct inode *inode,
  79. struct ocfs2_dinode *fe,
  80. char *name,
  81. struct buffer_head *de_bh,
  82. struct inode *orphan_dir_inode);
  83. static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
  84. handle_t *handle,
  85. struct inode *inode,
  86. const char *symname);
  87. /* An orphan dir name is an 8 byte value, printed as a hex string */
  88. #define OCFS2_ORPHAN_NAMELEN ((int)(2 * sizeof(u64)))
  89. static struct dentry *ocfs2_lookup(struct inode *dir, struct dentry *dentry,
  90. struct nameidata *nd)
  91. {
  92. int status;
  93. u64 blkno;
  94. struct inode *inode = NULL;
  95. struct dentry *ret;
  96. struct ocfs2_inode_info *oi;
  97. mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
  98. dentry->d_name.len, dentry->d_name.name);
  99. if (dentry->d_name.len > OCFS2_MAX_FILENAME_LEN) {
  100. ret = ERR_PTR(-ENAMETOOLONG);
  101. goto bail;
  102. }
  103. mlog(0, "find name %.*s in directory %llu\n", dentry->d_name.len,
  104. dentry->d_name.name, (unsigned long long)OCFS2_I(dir)->ip_blkno);
  105. status = ocfs2_inode_lock(dir, NULL, 0);
  106. if (status < 0) {
  107. if (status != -ENOENT)
  108. mlog_errno(status);
  109. ret = ERR_PTR(status);
  110. goto bail;
  111. }
  112. status = ocfs2_lookup_ino_from_name(dir, dentry->d_name.name,
  113. dentry->d_name.len, &blkno);
  114. if (status < 0)
  115. goto bail_add;
  116. inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0, 0);
  117. if (IS_ERR(inode)) {
  118. ret = ERR_PTR(-EACCES);
  119. goto bail_unlock;
  120. }
  121. oi = OCFS2_I(inode);
  122. /* Clear any orphaned state... If we were able to look up the
  123. * inode from a directory, it certainly can't be orphaned. We
  124. * might have the bad state from a node which intended to
  125. * orphan this inode but crashed before it could commit the
  126. * unlink. */
  127. spin_lock(&oi->ip_lock);
  128. oi->ip_flags &= ~OCFS2_INODE_MAYBE_ORPHANED;
  129. spin_unlock(&oi->ip_lock);
  130. bail_add:
  131. dentry->d_op = &ocfs2_dentry_ops;
  132. ret = d_splice_alias(inode, dentry);
  133. if (inode) {
  134. /*
  135. * If d_splice_alias() finds a DCACHE_DISCONNECTED
  136. * dentry, it will d_move() it on top of ourse. The
  137. * return value will indicate this however, so in
  138. * those cases, we switch them around for the locking
  139. * code.
  140. *
  141. * NOTE: This dentry already has ->d_op set from
  142. * ocfs2_get_parent() and ocfs2_get_dentry()
  143. */
  144. if (ret)
  145. dentry = ret;
  146. status = ocfs2_dentry_attach_lock(dentry, inode,
  147. OCFS2_I(dir)->ip_blkno);
  148. if (status) {
  149. mlog_errno(status);
  150. ret = ERR_PTR(status);
  151. goto bail_unlock;
  152. }
  153. }
  154. bail_unlock:
  155. /* Don't drop the cluster lock until *after* the d_add --
  156. * unlink on another node will message us to remove that
  157. * dentry under this lock so otherwise we can race this with
  158. * the downconvert thread and have a stale dentry. */
  159. ocfs2_inode_unlock(dir, 0);
  160. bail:
  161. mlog_exit_ptr(ret);
  162. return ret;
  163. }
  164. static struct inode *ocfs2_get_init_inode(struct inode *dir, int mode)
  165. {
  166. struct inode *inode;
  167. inode = new_inode(dir->i_sb);
  168. if (!inode) {
  169. mlog(ML_ERROR, "new_inode failed!\n");
  170. return NULL;
  171. }
  172. /* populate as many fields early on as possible - many of
  173. * these are used by the support functions here and in
  174. * callers. */
  175. if (S_ISDIR(mode))
  176. inode->i_nlink = 2;
  177. else
  178. inode->i_nlink = 1;
  179. inode->i_uid = current_fsuid();
  180. if (dir->i_mode & S_ISGID) {
  181. inode->i_gid = dir->i_gid;
  182. if (S_ISDIR(mode))
  183. mode |= S_ISGID;
  184. } else
  185. inode->i_gid = current_fsgid();
  186. inode->i_mode = mode;
  187. return inode;
  188. }
  189. static int ocfs2_mknod(struct inode *dir,
  190. struct dentry *dentry,
  191. int mode,
  192. dev_t dev)
  193. {
  194. int status = 0;
  195. struct buffer_head *parent_fe_bh = NULL;
  196. handle_t *handle = NULL;
  197. struct ocfs2_super *osb;
  198. struct ocfs2_dinode *dirfe;
  199. struct buffer_head *new_fe_bh = NULL;
  200. struct buffer_head *de_bh = NULL;
  201. struct inode *inode = NULL;
  202. struct ocfs2_alloc_context *inode_ac = NULL;
  203. struct ocfs2_alloc_context *data_ac = NULL;
  204. struct ocfs2_alloc_context *xattr_ac = NULL;
  205. int want_clusters = 0;
  206. int xattr_credits = 0;
  207. struct ocfs2_security_xattr_info si = {
  208. .enable = 1,
  209. };
  210. mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
  211. (unsigned long)dev, dentry->d_name.len,
  212. dentry->d_name.name);
  213. /* get our super block */
  214. osb = OCFS2_SB(dir->i_sb);
  215. status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
  216. if (status < 0) {
  217. if (status != -ENOENT)
  218. mlog_errno(status);
  219. return status;
  220. }
  221. if (S_ISDIR(mode) && (dir->i_nlink >= OCFS2_LINK_MAX)) {
  222. status = -EMLINK;
  223. goto leave;
  224. }
  225. dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
  226. if (!dirfe->i_links_count) {
  227. /* can't make a file in a deleted directory. */
  228. status = -ENOENT;
  229. goto leave;
  230. }
  231. status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
  232. dentry->d_name.len);
  233. if (status)
  234. goto leave;
  235. /* get a spot inside the dir. */
  236. status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
  237. dentry->d_name.name,
  238. dentry->d_name.len, &de_bh);
  239. if (status < 0) {
  240. mlog_errno(status);
  241. goto leave;
  242. }
  243. /* reserve an inode spot */
  244. status = ocfs2_reserve_new_inode(osb, &inode_ac);
  245. if (status < 0) {
  246. if (status != -ENOSPC)
  247. mlog_errno(status);
  248. goto leave;
  249. }
  250. inode = ocfs2_get_init_inode(dir, mode);
  251. if (!inode) {
  252. status = -ENOMEM;
  253. mlog_errno(status);
  254. goto leave;
  255. }
  256. /* get security xattr */
  257. status = ocfs2_init_security_get(inode, dir, &si);
  258. if (status) {
  259. if (status == -EOPNOTSUPP)
  260. si.enable = 0;
  261. else {
  262. mlog_errno(status);
  263. goto leave;
  264. }
  265. }
  266. /* calculate meta data/clusters for setting security and acl xattr */
  267. status = ocfs2_calc_xattr_init(dir, parent_fe_bh, mode,
  268. &si, &want_clusters,
  269. &xattr_credits, &xattr_ac);
  270. if (status < 0) {
  271. mlog_errno(status);
  272. goto leave;
  273. }
  274. /* Reserve a cluster if creating an extent based directory. */
  275. if (S_ISDIR(mode) && !ocfs2_supports_inline_data(osb))
  276. want_clusters += 1;
  277. status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac);
  278. if (status < 0) {
  279. if (status != -ENOSPC)
  280. mlog_errno(status);
  281. goto leave;
  282. }
  283. handle = ocfs2_start_trans(osb, OCFS2_MKNOD_CREDITS + xattr_credits);
  284. if (IS_ERR(handle)) {
  285. status = PTR_ERR(handle);
  286. handle = NULL;
  287. mlog_errno(status);
  288. goto leave;
  289. }
  290. /* do the real work now. */
  291. status = ocfs2_mknod_locked(osb, dir, inode, dentry, dev,
  292. &new_fe_bh, parent_fe_bh, handle,
  293. inode_ac);
  294. if (status < 0) {
  295. mlog_errno(status);
  296. goto leave;
  297. }
  298. if (S_ISDIR(mode)) {
  299. status = ocfs2_fill_new_dir(osb, handle, dir, inode,
  300. new_fe_bh, data_ac);
  301. if (status < 0) {
  302. mlog_errno(status);
  303. goto leave;
  304. }
  305. status = ocfs2_journal_access(handle, dir, parent_fe_bh,
  306. OCFS2_JOURNAL_ACCESS_WRITE);
  307. if (status < 0) {
  308. mlog_errno(status);
  309. goto leave;
  310. }
  311. le16_add_cpu(&dirfe->i_links_count, 1);
  312. status = ocfs2_journal_dirty(handle, parent_fe_bh);
  313. if (status < 0) {
  314. mlog_errno(status);
  315. goto leave;
  316. }
  317. inc_nlink(dir);
  318. }
  319. status = ocfs2_init_acl(handle, inode, dir, new_fe_bh, parent_fe_bh,
  320. xattr_ac, data_ac);
  321. if (status < 0) {
  322. mlog_errno(status);
  323. goto leave;
  324. }
  325. if (si.enable) {
  326. status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si,
  327. xattr_ac, data_ac);
  328. if (status < 0) {
  329. mlog_errno(status);
  330. goto leave;
  331. }
  332. }
  333. status = ocfs2_add_entry(handle, dentry, inode,
  334. OCFS2_I(inode)->ip_blkno, parent_fe_bh,
  335. de_bh);
  336. if (status < 0) {
  337. mlog_errno(status);
  338. goto leave;
  339. }
  340. status = ocfs2_dentry_attach_lock(dentry, inode,
  341. OCFS2_I(dir)->ip_blkno);
  342. if (status) {
  343. mlog_errno(status);
  344. goto leave;
  345. }
  346. insert_inode_hash(inode);
  347. dentry->d_op = &ocfs2_dentry_ops;
  348. d_instantiate(dentry, inode);
  349. status = 0;
  350. leave:
  351. if (handle)
  352. ocfs2_commit_trans(osb, handle);
  353. ocfs2_inode_unlock(dir, 1);
  354. if (status == -ENOSPC)
  355. mlog(0, "Disk is full\n");
  356. brelse(new_fe_bh);
  357. brelse(de_bh);
  358. brelse(parent_fe_bh);
  359. kfree(si.name);
  360. kfree(si.value);
  361. if ((status < 0) && inode) {
  362. clear_nlink(inode);
  363. iput(inode);
  364. }
  365. if (inode_ac)
  366. ocfs2_free_alloc_context(inode_ac);
  367. if (data_ac)
  368. ocfs2_free_alloc_context(data_ac);
  369. if (xattr_ac)
  370. ocfs2_free_alloc_context(xattr_ac);
  371. mlog_exit(status);
  372. return status;
  373. }
  374. static int ocfs2_mknod_locked(struct ocfs2_super *osb,
  375. struct inode *dir,
  376. struct inode *inode,
  377. struct dentry *dentry,
  378. dev_t dev,
  379. struct buffer_head **new_fe_bh,
  380. struct buffer_head *parent_fe_bh,
  381. handle_t *handle,
  382. struct ocfs2_alloc_context *inode_ac)
  383. {
  384. int status = 0;
  385. struct ocfs2_dinode *fe = NULL;
  386. struct ocfs2_extent_list *fel;
  387. u64 fe_blkno = 0;
  388. u16 suballoc_bit;
  389. mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry,
  390. inode->i_mode, (unsigned long)dev, dentry->d_name.len,
  391. dentry->d_name.name);
  392. *new_fe_bh = NULL;
  393. status = ocfs2_claim_new_inode(osb, handle, inode_ac, &suballoc_bit,
  394. &fe_blkno);
  395. if (status < 0) {
  396. mlog_errno(status);
  397. goto leave;
  398. }
  399. /* populate as many fields early on as possible - many of
  400. * these are used by the support functions here and in
  401. * callers. */
  402. inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
  403. OCFS2_I(inode)->ip_blkno = fe_blkno;
  404. spin_lock(&osb->osb_lock);
  405. inode->i_generation = osb->s_next_generation++;
  406. spin_unlock(&osb->osb_lock);
  407. *new_fe_bh = sb_getblk(osb->sb, fe_blkno);
  408. if (!*new_fe_bh) {
  409. status = -EIO;
  410. mlog_errno(status);
  411. goto leave;
  412. }
  413. ocfs2_set_new_buffer_uptodate(inode, *new_fe_bh);
  414. status = ocfs2_journal_access(handle, inode, *new_fe_bh,
  415. OCFS2_JOURNAL_ACCESS_CREATE);
  416. if (status < 0) {
  417. mlog_errno(status);
  418. goto leave;
  419. }
  420. fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data;
  421. memset(fe, 0, osb->sb->s_blocksize);
  422. fe->i_generation = cpu_to_le32(inode->i_generation);
  423. fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
  424. fe->i_blkno = cpu_to_le64(fe_blkno);
  425. fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
  426. fe->i_suballoc_slot = cpu_to_le16(inode_ac->ac_alloc_slot);
  427. fe->i_uid = cpu_to_le32(inode->i_uid);
  428. fe->i_gid = cpu_to_le32(inode->i_gid);
  429. fe->i_mode = cpu_to_le16(inode->i_mode);
  430. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
  431. fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
  432. fe->i_links_count = cpu_to_le16(inode->i_nlink);
  433. fe->i_last_eb_blk = 0;
  434. strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
  435. le32_add_cpu(&fe->i_flags, OCFS2_VALID_FL);
  436. fe->i_atime = fe->i_ctime = fe->i_mtime =
  437. cpu_to_le64(CURRENT_TIME.tv_sec);
  438. fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec =
  439. cpu_to_le32(CURRENT_TIME.tv_nsec);
  440. fe->i_dtime = 0;
  441. /*
  442. * If supported, directories start with inline data.
  443. */
  444. if (S_ISDIR(inode->i_mode) && ocfs2_supports_inline_data(osb)) {
  445. u16 feat = le16_to_cpu(fe->i_dyn_features);
  446. fe->i_dyn_features = cpu_to_le16(feat | OCFS2_INLINE_DATA_FL);
  447. fe->id2.i_data.id_count = cpu_to_le16(ocfs2_max_inline_data(osb->sb));
  448. } else {
  449. fel = &fe->id2.i_list;
  450. fel->l_tree_depth = 0;
  451. fel->l_next_free_rec = 0;
  452. fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb));
  453. }
  454. status = ocfs2_journal_dirty(handle, *new_fe_bh);
  455. if (status < 0) {
  456. mlog_errno(status);
  457. goto leave;
  458. }
  459. if (ocfs2_populate_inode(inode, fe, 1) < 0) {
  460. mlog(ML_ERROR, "populate inode failed! bh->b_blocknr=%llu, "
  461. "i_blkno=%llu, i_ino=%lu\n",
  462. (unsigned long long)(*new_fe_bh)->b_blocknr,
  463. (unsigned long long)le64_to_cpu(fe->i_blkno),
  464. inode->i_ino);
  465. BUG();
  466. }
  467. ocfs2_inode_set_new(osb, inode);
  468. if (!ocfs2_mount_local(osb)) {
  469. status = ocfs2_create_new_inode_locks(inode);
  470. if (status < 0)
  471. mlog_errno(status);
  472. }
  473. status = 0; /* error in ocfs2_create_new_inode_locks is not
  474. * critical */
  475. leave:
  476. if (status < 0) {
  477. if (*new_fe_bh) {
  478. brelse(*new_fe_bh);
  479. *new_fe_bh = NULL;
  480. }
  481. }
  482. mlog_exit(status);
  483. return status;
  484. }
  485. static int ocfs2_mkdir(struct inode *dir,
  486. struct dentry *dentry,
  487. int mode)
  488. {
  489. int ret;
  490. mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
  491. dentry->d_name.len, dentry->d_name.name);
  492. ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
  493. mlog_exit(ret);
  494. return ret;
  495. }
  496. static int ocfs2_create(struct inode *dir,
  497. struct dentry *dentry,
  498. int mode,
  499. struct nameidata *nd)
  500. {
  501. int ret;
  502. mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
  503. dentry->d_name.len, dentry->d_name.name);
  504. ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0);
  505. mlog_exit(ret);
  506. return ret;
  507. }
  508. static int ocfs2_link(struct dentry *old_dentry,
  509. struct inode *dir,
  510. struct dentry *dentry)
  511. {
  512. handle_t *handle;
  513. struct inode *inode = old_dentry->d_inode;
  514. int err;
  515. struct buffer_head *fe_bh = NULL;
  516. struct buffer_head *parent_fe_bh = NULL;
  517. struct buffer_head *de_bh = NULL;
  518. struct ocfs2_dinode *fe = NULL;
  519. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  520. mlog_entry("(inode=%lu, old='%.*s' new='%.*s')\n", inode->i_ino,
  521. old_dentry->d_name.len, old_dentry->d_name.name,
  522. dentry->d_name.len, dentry->d_name.name);
  523. if (S_ISDIR(inode->i_mode))
  524. return -EPERM;
  525. err = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
  526. if (err < 0) {
  527. if (err != -ENOENT)
  528. mlog_errno(err);
  529. return err;
  530. }
  531. if (!dir->i_nlink) {
  532. err = -ENOENT;
  533. goto out;
  534. }
  535. err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
  536. dentry->d_name.len);
  537. if (err)
  538. goto out;
  539. err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
  540. dentry->d_name.name,
  541. dentry->d_name.len, &de_bh);
  542. if (err < 0) {
  543. mlog_errno(err);
  544. goto out;
  545. }
  546. err = ocfs2_inode_lock(inode, &fe_bh, 1);
  547. if (err < 0) {
  548. if (err != -ENOENT)
  549. mlog_errno(err);
  550. goto out;
  551. }
  552. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  553. if (le16_to_cpu(fe->i_links_count) >= OCFS2_LINK_MAX) {
  554. err = -EMLINK;
  555. goto out_unlock_inode;
  556. }
  557. handle = ocfs2_start_trans(osb, OCFS2_LINK_CREDITS);
  558. if (IS_ERR(handle)) {
  559. err = PTR_ERR(handle);
  560. handle = NULL;
  561. mlog_errno(err);
  562. goto out_unlock_inode;
  563. }
  564. err = ocfs2_journal_access(handle, inode, fe_bh,
  565. OCFS2_JOURNAL_ACCESS_WRITE);
  566. if (err < 0) {
  567. mlog_errno(err);
  568. goto out_commit;
  569. }
  570. inc_nlink(inode);
  571. inode->i_ctime = CURRENT_TIME;
  572. fe->i_links_count = cpu_to_le16(inode->i_nlink);
  573. fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  574. fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  575. err = ocfs2_journal_dirty(handle, fe_bh);
  576. if (err < 0) {
  577. le16_add_cpu(&fe->i_links_count, -1);
  578. drop_nlink(inode);
  579. mlog_errno(err);
  580. goto out_commit;
  581. }
  582. err = ocfs2_add_entry(handle, dentry, inode,
  583. OCFS2_I(inode)->ip_blkno,
  584. parent_fe_bh, de_bh);
  585. if (err) {
  586. le16_add_cpu(&fe->i_links_count, -1);
  587. drop_nlink(inode);
  588. mlog_errno(err);
  589. goto out_commit;
  590. }
  591. err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
  592. if (err) {
  593. mlog_errno(err);
  594. goto out_commit;
  595. }
  596. atomic_inc(&inode->i_count);
  597. dentry->d_op = &ocfs2_dentry_ops;
  598. d_instantiate(dentry, inode);
  599. out_commit:
  600. ocfs2_commit_trans(osb, handle);
  601. out_unlock_inode:
  602. ocfs2_inode_unlock(inode, 1);
  603. out:
  604. ocfs2_inode_unlock(dir, 1);
  605. brelse(de_bh);
  606. brelse(fe_bh);
  607. brelse(parent_fe_bh);
  608. mlog_exit(err);
  609. return err;
  610. }
  611. /*
  612. * Takes and drops an exclusive lock on the given dentry. This will
  613. * force other nodes to drop it.
  614. */
  615. static int ocfs2_remote_dentry_delete(struct dentry *dentry)
  616. {
  617. int ret;
  618. ret = ocfs2_dentry_lock(dentry, 1);
  619. if (ret)
  620. mlog_errno(ret);
  621. else
  622. ocfs2_dentry_unlock(dentry, 1);
  623. return ret;
  624. }
  625. static inline int inode_is_unlinkable(struct inode *inode)
  626. {
  627. if (S_ISDIR(inode->i_mode)) {
  628. if (inode->i_nlink == 2)
  629. return 1;
  630. return 0;
  631. }
  632. if (inode->i_nlink == 1)
  633. return 1;
  634. return 0;
  635. }
  636. static int ocfs2_unlink(struct inode *dir,
  637. struct dentry *dentry)
  638. {
  639. int status;
  640. int child_locked = 0;
  641. struct inode *inode = dentry->d_inode;
  642. struct inode *orphan_dir = NULL;
  643. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  644. u64 blkno;
  645. struct ocfs2_dinode *fe = NULL;
  646. struct buffer_head *fe_bh = NULL;
  647. struct buffer_head *parent_node_bh = NULL;
  648. handle_t *handle = NULL;
  649. struct ocfs2_dir_entry *dirent = NULL;
  650. struct buffer_head *dirent_bh = NULL;
  651. char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
  652. struct buffer_head *orphan_entry_bh = NULL;
  653. mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
  654. dentry->d_name.len, dentry->d_name.name);
  655. BUG_ON(dentry->d_parent->d_inode != dir);
  656. mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
  657. if (inode == osb->root_inode) {
  658. mlog(0, "Cannot delete the root directory\n");
  659. return -EPERM;
  660. }
  661. status = ocfs2_inode_lock(dir, &parent_node_bh, 1);
  662. if (status < 0) {
  663. if (status != -ENOENT)
  664. mlog_errno(status);
  665. return status;
  666. }
  667. status = ocfs2_find_files_on_disk(dentry->d_name.name,
  668. dentry->d_name.len, &blkno,
  669. dir, &dirent_bh, &dirent);
  670. if (status < 0) {
  671. if (status != -ENOENT)
  672. mlog_errno(status);
  673. goto leave;
  674. }
  675. if (OCFS2_I(inode)->ip_blkno != blkno) {
  676. status = -ENOENT;
  677. mlog(0, "ip_blkno %llu != dirent blkno %llu ip_flags = %x\n",
  678. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  679. (unsigned long long)blkno, OCFS2_I(inode)->ip_flags);
  680. goto leave;
  681. }
  682. status = ocfs2_inode_lock(inode, &fe_bh, 1);
  683. if (status < 0) {
  684. if (status != -ENOENT)
  685. mlog_errno(status);
  686. goto leave;
  687. }
  688. child_locked = 1;
  689. if (S_ISDIR(inode->i_mode)) {
  690. if (!ocfs2_empty_dir(inode)) {
  691. status = -ENOTEMPTY;
  692. goto leave;
  693. } else if (inode->i_nlink != 2) {
  694. status = -ENOTEMPTY;
  695. goto leave;
  696. }
  697. }
  698. status = ocfs2_remote_dentry_delete(dentry);
  699. if (status < 0) {
  700. /* This remote delete should succeed under all normal
  701. * circumstances. */
  702. mlog_errno(status);
  703. goto leave;
  704. }
  705. if (inode_is_unlinkable(inode)) {
  706. status = ocfs2_prepare_orphan_dir(osb, &orphan_dir, inode,
  707. orphan_name,
  708. &orphan_entry_bh);
  709. if (status < 0) {
  710. mlog_errno(status);
  711. goto leave;
  712. }
  713. }
  714. handle = ocfs2_start_trans(osb, OCFS2_UNLINK_CREDITS);
  715. if (IS_ERR(handle)) {
  716. status = PTR_ERR(handle);
  717. handle = NULL;
  718. mlog_errno(status);
  719. goto leave;
  720. }
  721. status = ocfs2_journal_access(handle, inode, fe_bh,
  722. OCFS2_JOURNAL_ACCESS_WRITE);
  723. if (status < 0) {
  724. mlog_errno(status);
  725. goto leave;
  726. }
  727. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  728. if (inode_is_unlinkable(inode)) {
  729. status = ocfs2_orphan_add(osb, handle, inode, fe, orphan_name,
  730. orphan_entry_bh, orphan_dir);
  731. if (status < 0) {
  732. mlog_errno(status);
  733. goto leave;
  734. }
  735. }
  736. /* delete the name from the parent dir */
  737. status = ocfs2_delete_entry(handle, dir, dirent, dirent_bh);
  738. if (status < 0) {
  739. mlog_errno(status);
  740. goto leave;
  741. }
  742. if (S_ISDIR(inode->i_mode))
  743. drop_nlink(inode);
  744. drop_nlink(inode);
  745. fe->i_links_count = cpu_to_le16(inode->i_nlink);
  746. status = ocfs2_journal_dirty(handle, fe_bh);
  747. if (status < 0) {
  748. mlog_errno(status);
  749. goto leave;
  750. }
  751. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  752. if (S_ISDIR(inode->i_mode))
  753. drop_nlink(dir);
  754. status = ocfs2_mark_inode_dirty(handle, dir, parent_node_bh);
  755. if (status < 0) {
  756. mlog_errno(status);
  757. if (S_ISDIR(inode->i_mode))
  758. inc_nlink(dir);
  759. }
  760. leave:
  761. if (handle)
  762. ocfs2_commit_trans(osb, handle);
  763. if (child_locked)
  764. ocfs2_inode_unlock(inode, 1);
  765. ocfs2_inode_unlock(dir, 1);
  766. if (orphan_dir) {
  767. /* This was locked for us in ocfs2_prepare_orphan_dir() */
  768. ocfs2_inode_unlock(orphan_dir, 1);
  769. mutex_unlock(&orphan_dir->i_mutex);
  770. iput(orphan_dir);
  771. }
  772. brelse(fe_bh);
  773. brelse(dirent_bh);
  774. brelse(parent_node_bh);
  775. brelse(orphan_entry_bh);
  776. mlog_exit(status);
  777. return status;
  778. }
  779. /*
  780. * The only place this should be used is rename!
  781. * if they have the same id, then the 1st one is the only one locked.
  782. */
  783. static int ocfs2_double_lock(struct ocfs2_super *osb,
  784. struct buffer_head **bh1,
  785. struct inode *inode1,
  786. struct buffer_head **bh2,
  787. struct inode *inode2)
  788. {
  789. int status;
  790. struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
  791. struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
  792. struct buffer_head **tmpbh;
  793. struct inode *tmpinode;
  794. mlog_entry("(inode1 = %llu, inode2 = %llu)\n",
  795. (unsigned long long)oi1->ip_blkno,
  796. (unsigned long long)oi2->ip_blkno);
  797. if (*bh1)
  798. *bh1 = NULL;
  799. if (*bh2)
  800. *bh2 = NULL;
  801. /* we always want to lock the one with the lower lockid first. */
  802. if (oi1->ip_blkno != oi2->ip_blkno) {
  803. if (oi1->ip_blkno < oi2->ip_blkno) {
  804. /* switch id1 and id2 around */
  805. mlog(0, "switching them around...\n");
  806. tmpbh = bh2;
  807. bh2 = bh1;
  808. bh1 = tmpbh;
  809. tmpinode = inode2;
  810. inode2 = inode1;
  811. inode1 = tmpinode;
  812. }
  813. /* lock id2 */
  814. status = ocfs2_inode_lock(inode2, bh2, 1);
  815. if (status < 0) {
  816. if (status != -ENOENT)
  817. mlog_errno(status);
  818. goto bail;
  819. }
  820. }
  821. /* lock id1 */
  822. status = ocfs2_inode_lock(inode1, bh1, 1);
  823. if (status < 0) {
  824. /*
  825. * An error return must mean that no cluster locks
  826. * were held on function exit.
  827. */
  828. if (oi1->ip_blkno != oi2->ip_blkno)
  829. ocfs2_inode_unlock(inode2, 1);
  830. if (status != -ENOENT)
  831. mlog_errno(status);
  832. }
  833. bail:
  834. mlog_exit(status);
  835. return status;
  836. }
  837. static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2)
  838. {
  839. ocfs2_inode_unlock(inode1, 1);
  840. if (inode1 != inode2)
  841. ocfs2_inode_unlock(inode2, 1);
  842. }
  843. static int ocfs2_rename(struct inode *old_dir,
  844. struct dentry *old_dentry,
  845. struct inode *new_dir,
  846. struct dentry *new_dentry)
  847. {
  848. int status = 0, rename_lock = 0, parents_locked = 0;
  849. int old_child_locked = 0, new_child_locked = 0;
  850. struct inode *old_inode = old_dentry->d_inode;
  851. struct inode *new_inode = new_dentry->d_inode;
  852. struct inode *orphan_dir = NULL;
  853. struct ocfs2_dinode *newfe = NULL;
  854. char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
  855. struct buffer_head *orphan_entry_bh = NULL;
  856. struct buffer_head *newfe_bh = NULL;
  857. struct buffer_head *old_inode_bh = NULL;
  858. struct buffer_head *insert_entry_bh = NULL;
  859. struct ocfs2_super *osb = NULL;
  860. u64 newfe_blkno, old_de_ino;
  861. handle_t *handle = NULL;
  862. struct buffer_head *old_dir_bh = NULL;
  863. struct buffer_head *new_dir_bh = NULL;
  864. struct ocfs2_dir_entry *old_inode_dot_dot_de = NULL, *old_de = NULL,
  865. *new_de = NULL;
  866. struct buffer_head *new_de_bh = NULL, *old_de_bh = NULL; // bhs for above
  867. struct buffer_head *old_inode_de_bh = NULL; // if old_dentry is a dir,
  868. // this is the 1st dirent bh
  869. nlink_t old_dir_nlink = old_dir->i_nlink;
  870. struct ocfs2_dinode *old_di;
  871. /* At some point it might be nice to break this function up a
  872. * bit. */
  873. mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p, from='%.*s' to='%.*s')\n",
  874. old_dir, old_dentry, new_dir, new_dentry,
  875. old_dentry->d_name.len, old_dentry->d_name.name,
  876. new_dentry->d_name.len, new_dentry->d_name.name);
  877. osb = OCFS2_SB(old_dir->i_sb);
  878. if (new_inode) {
  879. if (!igrab(new_inode))
  880. BUG();
  881. }
  882. /* Assume a directory hierarchy thusly:
  883. * a/b/c
  884. * a/d
  885. * a,b,c, and d are all directories.
  886. *
  887. * from cwd of 'a' on both nodes:
  888. * node1: mv b/c d
  889. * node2: mv d b/c
  890. *
  891. * And that's why, just like the VFS, we need a file system
  892. * rename lock. */
  893. if (old_dir != new_dir && S_ISDIR(old_inode->i_mode)) {
  894. status = ocfs2_rename_lock(osb);
  895. if (status < 0) {
  896. mlog_errno(status);
  897. goto bail;
  898. }
  899. rename_lock = 1;
  900. }
  901. /* if old and new are the same, this'll just do one lock. */
  902. status = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
  903. &new_dir_bh, new_dir);
  904. if (status < 0) {
  905. mlog_errno(status);
  906. goto bail;
  907. }
  908. parents_locked = 1;
  909. /* make sure both dirs have bhs
  910. * get an extra ref on old_dir_bh if old==new */
  911. if (!new_dir_bh) {
  912. if (old_dir_bh) {
  913. new_dir_bh = old_dir_bh;
  914. get_bh(new_dir_bh);
  915. } else {
  916. mlog(ML_ERROR, "no old_dir_bh!\n");
  917. status = -EIO;
  918. goto bail;
  919. }
  920. }
  921. /*
  922. * Aside from allowing a meta data update, the locking here
  923. * also ensures that the downconvert thread on other nodes
  924. * won't have to concurrently downconvert the inode and the
  925. * dentry locks.
  926. */
  927. status = ocfs2_inode_lock(old_inode, &old_inode_bh, 1);
  928. if (status < 0) {
  929. if (status != -ENOENT)
  930. mlog_errno(status);
  931. goto bail;
  932. }
  933. old_child_locked = 1;
  934. status = ocfs2_remote_dentry_delete(old_dentry);
  935. if (status < 0) {
  936. mlog_errno(status);
  937. goto bail;
  938. }
  939. if (S_ISDIR(old_inode->i_mode)) {
  940. u64 old_inode_parent;
  941. status = ocfs2_find_files_on_disk("..", 2, &old_inode_parent,
  942. old_inode, &old_inode_de_bh,
  943. &old_inode_dot_dot_de);
  944. if (status) {
  945. status = -EIO;
  946. goto bail;
  947. }
  948. if (old_inode_parent != OCFS2_I(old_dir)->ip_blkno) {
  949. status = -EIO;
  950. goto bail;
  951. }
  952. if (!new_inode && new_dir != old_dir &&
  953. new_dir->i_nlink >= OCFS2_LINK_MAX) {
  954. status = -EMLINK;
  955. goto bail;
  956. }
  957. }
  958. status = ocfs2_lookup_ino_from_name(old_dir, old_dentry->d_name.name,
  959. old_dentry->d_name.len,
  960. &old_de_ino);
  961. if (status) {
  962. status = -ENOENT;
  963. goto bail;
  964. }
  965. /*
  966. * Check for inode number is _not_ due to possible IO errors.
  967. * We might rmdir the source, keep it as pwd of some process
  968. * and merrily kill the link to whatever was created under the
  969. * same name. Goodbye sticky bit ;-<
  970. */
  971. if (old_de_ino != OCFS2_I(old_inode)->ip_blkno) {
  972. status = -ENOENT;
  973. goto bail;
  974. }
  975. /* check if the target already exists (in which case we need
  976. * to delete it */
  977. status = ocfs2_find_files_on_disk(new_dentry->d_name.name,
  978. new_dentry->d_name.len,
  979. &newfe_blkno, new_dir, &new_de_bh,
  980. &new_de);
  981. /* The only error we allow here is -ENOENT because the new
  982. * file not existing is perfectly valid. */
  983. if ((status < 0) && (status != -ENOENT)) {
  984. /* If we cannot find the file specified we should just */
  985. /* return the error... */
  986. mlog_errno(status);
  987. goto bail;
  988. }
  989. if (!new_de && new_inode) {
  990. /*
  991. * Target was unlinked by another node while we were
  992. * waiting to get to ocfs2_rename(). There isn't
  993. * anything we can do here to help the situation, so
  994. * bubble up the appropriate error.
  995. */
  996. status = -ENOENT;
  997. goto bail;
  998. }
  999. /* In case we need to overwrite an existing file, we blow it
  1000. * away first */
  1001. if (new_de) {
  1002. /* VFS didn't think there existed an inode here, but
  1003. * someone else in the cluster must have raced our
  1004. * rename to create one. Today we error cleanly, in
  1005. * the future we should consider calling iget to build
  1006. * a new struct inode for this entry. */
  1007. if (!new_inode) {
  1008. status = -EACCES;
  1009. mlog(0, "We found an inode for name %.*s but VFS "
  1010. "didn't give us one.\n", new_dentry->d_name.len,
  1011. new_dentry->d_name.name);
  1012. goto bail;
  1013. }
  1014. if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
  1015. status = -EACCES;
  1016. mlog(0, "Inode %llu and dir %llu disagree. flags = %x\n",
  1017. (unsigned long long)OCFS2_I(new_inode)->ip_blkno,
  1018. (unsigned long long)newfe_blkno,
  1019. OCFS2_I(new_inode)->ip_flags);
  1020. goto bail;
  1021. }
  1022. status = ocfs2_inode_lock(new_inode, &newfe_bh, 1);
  1023. if (status < 0) {
  1024. if (status != -ENOENT)
  1025. mlog_errno(status);
  1026. goto bail;
  1027. }
  1028. new_child_locked = 1;
  1029. status = ocfs2_remote_dentry_delete(new_dentry);
  1030. if (status < 0) {
  1031. mlog_errno(status);
  1032. goto bail;
  1033. }
  1034. newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
  1035. mlog(0, "aha rename over existing... new_de=%p new_blkno=%llu "
  1036. "newfebh=%p bhblocknr=%llu\n", new_de,
  1037. (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
  1038. (unsigned long long)newfe_bh->b_blocknr : 0ULL);
  1039. if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
  1040. status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
  1041. new_inode,
  1042. orphan_name,
  1043. &orphan_entry_bh);
  1044. if (status < 0) {
  1045. mlog_errno(status);
  1046. goto bail;
  1047. }
  1048. }
  1049. } else {
  1050. BUG_ON(new_dentry->d_parent->d_inode != new_dir);
  1051. status = ocfs2_check_dir_for_entry(new_dir,
  1052. new_dentry->d_name.name,
  1053. new_dentry->d_name.len);
  1054. if (status)
  1055. goto bail;
  1056. status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,
  1057. new_dentry->d_name.name,
  1058. new_dentry->d_name.len,
  1059. &insert_entry_bh);
  1060. if (status < 0) {
  1061. mlog_errno(status);
  1062. goto bail;
  1063. }
  1064. }
  1065. handle = ocfs2_start_trans(osb, OCFS2_RENAME_CREDITS);
  1066. if (IS_ERR(handle)) {
  1067. status = PTR_ERR(handle);
  1068. handle = NULL;
  1069. mlog_errno(status);
  1070. goto bail;
  1071. }
  1072. if (new_de) {
  1073. if (S_ISDIR(new_inode->i_mode)) {
  1074. if (!ocfs2_empty_dir(new_inode) ||
  1075. new_inode->i_nlink != 2) {
  1076. status = -ENOTEMPTY;
  1077. goto bail;
  1078. }
  1079. }
  1080. status = ocfs2_journal_access(handle, new_inode, newfe_bh,
  1081. OCFS2_JOURNAL_ACCESS_WRITE);
  1082. if (status < 0) {
  1083. mlog_errno(status);
  1084. goto bail;
  1085. }
  1086. if (S_ISDIR(new_inode->i_mode) ||
  1087. (newfe->i_links_count == cpu_to_le16(1))){
  1088. status = ocfs2_orphan_add(osb, handle, new_inode,
  1089. newfe, orphan_name,
  1090. orphan_entry_bh, orphan_dir);
  1091. if (status < 0) {
  1092. mlog_errno(status);
  1093. goto bail;
  1094. }
  1095. }
  1096. /* change the dirent to point to the correct inode */
  1097. status = ocfs2_update_entry(new_dir, handle, new_de_bh,
  1098. new_de, old_inode);
  1099. if (status < 0) {
  1100. mlog_errno(status);
  1101. goto bail;
  1102. }
  1103. new_dir->i_version++;
  1104. if (S_ISDIR(new_inode->i_mode))
  1105. newfe->i_links_count = 0;
  1106. else
  1107. le16_add_cpu(&newfe->i_links_count, -1);
  1108. status = ocfs2_journal_dirty(handle, newfe_bh);
  1109. if (status < 0) {
  1110. mlog_errno(status);
  1111. goto bail;
  1112. }
  1113. } else {
  1114. /* if the name was not found in new_dir, add it now */
  1115. status = ocfs2_add_entry(handle, new_dentry, old_inode,
  1116. OCFS2_I(old_inode)->ip_blkno,
  1117. new_dir_bh, insert_entry_bh);
  1118. }
  1119. old_inode->i_ctime = CURRENT_TIME;
  1120. mark_inode_dirty(old_inode);
  1121. status = ocfs2_journal_access(handle, old_inode, old_inode_bh,
  1122. OCFS2_JOURNAL_ACCESS_WRITE);
  1123. if (status >= 0) {
  1124. old_di = (struct ocfs2_dinode *) old_inode_bh->b_data;
  1125. old_di->i_ctime = cpu_to_le64(old_inode->i_ctime.tv_sec);
  1126. old_di->i_ctime_nsec = cpu_to_le32(old_inode->i_ctime.tv_nsec);
  1127. status = ocfs2_journal_dirty(handle, old_inode_bh);
  1128. if (status < 0)
  1129. mlog_errno(status);
  1130. } else
  1131. mlog_errno(status);
  1132. /*
  1133. * Now that the name has been added to new_dir, remove the old name.
  1134. *
  1135. * We don't keep any directory entry context around until now
  1136. * because the insert might have changed the type of directory
  1137. * we're dealing with.
  1138. */
  1139. old_de_bh = ocfs2_find_entry(old_dentry->d_name.name,
  1140. old_dentry->d_name.len,
  1141. old_dir, &old_de);
  1142. if (!old_de_bh) {
  1143. status = -EIO;
  1144. goto bail;
  1145. }
  1146. status = ocfs2_delete_entry(handle, old_dir, old_de, old_de_bh);
  1147. if (status < 0) {
  1148. mlog_errno(status);
  1149. goto bail;
  1150. }
  1151. if (new_inode) {
  1152. new_inode->i_nlink--;
  1153. new_inode->i_ctime = CURRENT_TIME;
  1154. }
  1155. old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
  1156. if (old_inode_de_bh) {
  1157. status = ocfs2_update_entry(old_inode, handle, old_inode_de_bh,
  1158. old_inode_dot_dot_de, new_dir);
  1159. old_dir->i_nlink--;
  1160. if (new_inode) {
  1161. new_inode->i_nlink--;
  1162. } else {
  1163. inc_nlink(new_dir);
  1164. mark_inode_dirty(new_dir);
  1165. }
  1166. }
  1167. mark_inode_dirty(old_dir);
  1168. ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh);
  1169. if (new_inode) {
  1170. mark_inode_dirty(new_inode);
  1171. ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh);
  1172. }
  1173. if (old_dir != new_dir) {
  1174. /* Keep the same times on both directories.*/
  1175. new_dir->i_ctime = new_dir->i_mtime = old_dir->i_ctime;
  1176. /*
  1177. * This will also pick up the i_nlink change from the
  1178. * block above.
  1179. */
  1180. ocfs2_mark_inode_dirty(handle, new_dir, new_dir_bh);
  1181. }
  1182. if (old_dir_nlink != old_dir->i_nlink) {
  1183. if (!old_dir_bh) {
  1184. mlog(ML_ERROR, "need to change nlink for old dir "
  1185. "%llu from %d to %d but bh is NULL!\n",
  1186. (unsigned long long)OCFS2_I(old_dir)->ip_blkno,
  1187. (int)old_dir_nlink, old_dir->i_nlink);
  1188. } else {
  1189. struct ocfs2_dinode *fe;
  1190. status = ocfs2_journal_access(handle, old_dir,
  1191. old_dir_bh,
  1192. OCFS2_JOURNAL_ACCESS_WRITE);
  1193. fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
  1194. fe->i_links_count = cpu_to_le16(old_dir->i_nlink);
  1195. status = ocfs2_journal_dirty(handle, old_dir_bh);
  1196. }
  1197. }
  1198. ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
  1199. status = 0;
  1200. bail:
  1201. if (rename_lock)
  1202. ocfs2_rename_unlock(osb);
  1203. if (handle)
  1204. ocfs2_commit_trans(osb, handle);
  1205. if (parents_locked)
  1206. ocfs2_double_unlock(old_dir, new_dir);
  1207. if (old_child_locked)
  1208. ocfs2_inode_unlock(old_inode, 1);
  1209. if (new_child_locked)
  1210. ocfs2_inode_unlock(new_inode, 1);
  1211. if (orphan_dir) {
  1212. /* This was locked for us in ocfs2_prepare_orphan_dir() */
  1213. ocfs2_inode_unlock(orphan_dir, 1);
  1214. mutex_unlock(&orphan_dir->i_mutex);
  1215. iput(orphan_dir);
  1216. }
  1217. if (new_inode)
  1218. sync_mapping_buffers(old_inode->i_mapping);
  1219. if (new_inode)
  1220. iput(new_inode);
  1221. brelse(newfe_bh);
  1222. brelse(old_inode_bh);
  1223. brelse(old_dir_bh);
  1224. brelse(new_dir_bh);
  1225. brelse(new_de_bh);
  1226. brelse(old_de_bh);
  1227. brelse(old_inode_de_bh);
  1228. brelse(orphan_entry_bh);
  1229. brelse(insert_entry_bh);
  1230. mlog_exit(status);
  1231. return status;
  1232. }
  1233. /*
  1234. * we expect i_size = strlen(symname). Copy symname into the file
  1235. * data, including the null terminator.
  1236. */
  1237. static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
  1238. handle_t *handle,
  1239. struct inode *inode,
  1240. const char *symname)
  1241. {
  1242. struct buffer_head **bhs = NULL;
  1243. const char *c;
  1244. struct super_block *sb = osb->sb;
  1245. u64 p_blkno, p_blocks;
  1246. int virtual, blocks, status, i, bytes_left;
  1247. bytes_left = i_size_read(inode) + 1;
  1248. /* we can't trust i_blocks because we're actually going to
  1249. * write i_size + 1 bytes. */
  1250. blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
  1251. mlog_entry("i_blocks = %llu, i_size = %llu, blocks = %d\n",
  1252. (unsigned long long)inode->i_blocks,
  1253. i_size_read(inode), blocks);
  1254. /* Sanity check -- make sure we're going to fit. */
  1255. if (bytes_left >
  1256. ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
  1257. status = -EIO;
  1258. mlog_errno(status);
  1259. goto bail;
  1260. }
  1261. bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
  1262. if (!bhs) {
  1263. status = -ENOMEM;
  1264. mlog_errno(status);
  1265. goto bail;
  1266. }
  1267. status = ocfs2_extent_map_get_blocks(inode, 0, &p_blkno, &p_blocks,
  1268. NULL);
  1269. if (status < 0) {
  1270. mlog_errno(status);
  1271. goto bail;
  1272. }
  1273. /* links can never be larger than one cluster so we know this
  1274. * is all going to be contiguous, but do a sanity check
  1275. * anyway. */
  1276. if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
  1277. status = -EIO;
  1278. mlog_errno(status);
  1279. goto bail;
  1280. }
  1281. virtual = 0;
  1282. while(bytes_left > 0) {
  1283. c = &symname[virtual * sb->s_blocksize];
  1284. bhs[virtual] = sb_getblk(sb, p_blkno);
  1285. if (!bhs[virtual]) {
  1286. status = -ENOMEM;
  1287. mlog_errno(status);
  1288. goto bail;
  1289. }
  1290. ocfs2_set_new_buffer_uptodate(inode, bhs[virtual]);
  1291. status = ocfs2_journal_access(handle, inode, bhs[virtual],
  1292. OCFS2_JOURNAL_ACCESS_CREATE);
  1293. if (status < 0) {
  1294. mlog_errno(status);
  1295. goto bail;
  1296. }
  1297. memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
  1298. memcpy(bhs[virtual]->b_data, c,
  1299. (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
  1300. bytes_left);
  1301. status = ocfs2_journal_dirty(handle, bhs[virtual]);
  1302. if (status < 0) {
  1303. mlog_errno(status);
  1304. goto bail;
  1305. }
  1306. virtual++;
  1307. p_blkno++;
  1308. bytes_left -= sb->s_blocksize;
  1309. }
  1310. status = 0;
  1311. bail:
  1312. if (bhs) {
  1313. for(i = 0; i < blocks; i++)
  1314. brelse(bhs[i]);
  1315. kfree(bhs);
  1316. }
  1317. mlog_exit(status);
  1318. return status;
  1319. }
  1320. static int ocfs2_symlink(struct inode *dir,
  1321. struct dentry *dentry,
  1322. const char *symname)
  1323. {
  1324. int status, l, credits;
  1325. u64 newsize;
  1326. struct ocfs2_super *osb = NULL;
  1327. struct inode *inode = NULL;
  1328. struct super_block *sb;
  1329. struct buffer_head *new_fe_bh = NULL;
  1330. struct buffer_head *de_bh = NULL;
  1331. struct buffer_head *parent_fe_bh = NULL;
  1332. struct ocfs2_dinode *fe = NULL;
  1333. struct ocfs2_dinode *dirfe;
  1334. handle_t *handle = NULL;
  1335. struct ocfs2_alloc_context *inode_ac = NULL;
  1336. struct ocfs2_alloc_context *data_ac = NULL;
  1337. struct ocfs2_alloc_context *xattr_ac = NULL;
  1338. int want_clusters = 0;
  1339. int xattr_credits = 0;
  1340. struct ocfs2_security_xattr_info si = {
  1341. .enable = 1,
  1342. };
  1343. mlog_entry("(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir,
  1344. dentry, symname, dentry->d_name.len, dentry->d_name.name);
  1345. sb = dir->i_sb;
  1346. osb = OCFS2_SB(sb);
  1347. l = strlen(symname) + 1;
  1348. credits = ocfs2_calc_symlink_credits(sb);
  1349. /* lock the parent directory */
  1350. status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
  1351. if (status < 0) {
  1352. if (status != -ENOENT)
  1353. mlog_errno(status);
  1354. return status;
  1355. }
  1356. dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
  1357. if (!dirfe->i_links_count) {
  1358. /* can't make a file in a deleted directory. */
  1359. status = -ENOENT;
  1360. goto bail;
  1361. }
  1362. status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
  1363. dentry->d_name.len);
  1364. if (status)
  1365. goto bail;
  1366. status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
  1367. dentry->d_name.name,
  1368. dentry->d_name.len, &de_bh);
  1369. if (status < 0) {
  1370. mlog_errno(status);
  1371. goto bail;
  1372. }
  1373. status = ocfs2_reserve_new_inode(osb, &inode_ac);
  1374. if (status < 0) {
  1375. if (status != -ENOSPC)
  1376. mlog_errno(status);
  1377. goto bail;
  1378. }
  1379. inode = ocfs2_get_init_inode(dir, S_IFLNK | S_IRWXUGO);
  1380. if (!inode) {
  1381. status = -ENOMEM;
  1382. mlog_errno(status);
  1383. goto bail;
  1384. }
  1385. /* get security xattr */
  1386. status = ocfs2_init_security_get(inode, dir, &si);
  1387. if (status) {
  1388. if (status == -EOPNOTSUPP)
  1389. si.enable = 0;
  1390. else {
  1391. mlog_errno(status);
  1392. goto bail;
  1393. }
  1394. }
  1395. /* calculate meta data/clusters for setting security xattr */
  1396. if (si.enable) {
  1397. status = ocfs2_calc_security_init(dir, &si, &want_clusters,
  1398. &xattr_credits, &xattr_ac);
  1399. if (status < 0) {
  1400. mlog_errno(status);
  1401. goto bail;
  1402. }
  1403. }
  1404. /* don't reserve bitmap space for fast symlinks. */
  1405. if (l > ocfs2_fast_symlink_chars(sb))
  1406. want_clusters += 1;
  1407. status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac);
  1408. if (status < 0) {
  1409. if (status != -ENOSPC)
  1410. mlog_errno(status);
  1411. goto bail;
  1412. }
  1413. handle = ocfs2_start_trans(osb, credits + xattr_credits);
  1414. if (IS_ERR(handle)) {
  1415. status = PTR_ERR(handle);
  1416. handle = NULL;
  1417. mlog_errno(status);
  1418. goto bail;
  1419. }
  1420. status = ocfs2_mknod_locked(osb, dir, inode, dentry,
  1421. 0, &new_fe_bh, parent_fe_bh, handle,
  1422. inode_ac);
  1423. if (status < 0) {
  1424. mlog_errno(status);
  1425. goto bail;
  1426. }
  1427. fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
  1428. inode->i_rdev = 0;
  1429. newsize = l - 1;
  1430. if (l > ocfs2_fast_symlink_chars(sb)) {
  1431. u32 offset = 0;
  1432. inode->i_op = &ocfs2_symlink_inode_operations;
  1433. status = ocfs2_add_inode_data(osb, inode, &offset, 1, 0,
  1434. new_fe_bh,
  1435. handle, data_ac, NULL,
  1436. NULL);
  1437. if (status < 0) {
  1438. if (status != -ENOSPC && status != -EINTR) {
  1439. mlog(ML_ERROR,
  1440. "Failed to extend file to %llu\n",
  1441. (unsigned long long)newsize);
  1442. mlog_errno(status);
  1443. status = -ENOSPC;
  1444. }
  1445. goto bail;
  1446. }
  1447. i_size_write(inode, newsize);
  1448. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1449. } else {
  1450. inode->i_op = &ocfs2_fast_symlink_inode_operations;
  1451. memcpy((char *) fe->id2.i_symlink, symname, l);
  1452. i_size_write(inode, newsize);
  1453. inode->i_blocks = 0;
  1454. }
  1455. status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
  1456. if (status < 0) {
  1457. mlog_errno(status);
  1458. goto bail;
  1459. }
  1460. if (!ocfs2_inode_is_fast_symlink(inode)) {
  1461. status = ocfs2_create_symlink_data(osb, handle, inode,
  1462. symname);
  1463. if (status < 0) {
  1464. mlog_errno(status);
  1465. goto bail;
  1466. }
  1467. }
  1468. if (si.enable) {
  1469. status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si,
  1470. xattr_ac, data_ac);
  1471. if (status < 0) {
  1472. mlog_errno(status);
  1473. goto bail;
  1474. }
  1475. }
  1476. status = ocfs2_add_entry(handle, dentry, inode,
  1477. le64_to_cpu(fe->i_blkno), parent_fe_bh,
  1478. de_bh);
  1479. if (status < 0) {
  1480. mlog_errno(status);
  1481. goto bail;
  1482. }
  1483. status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
  1484. if (status) {
  1485. mlog_errno(status);
  1486. goto bail;
  1487. }
  1488. insert_inode_hash(inode);
  1489. dentry->d_op = &ocfs2_dentry_ops;
  1490. d_instantiate(dentry, inode);
  1491. bail:
  1492. if (handle)
  1493. ocfs2_commit_trans(osb, handle);
  1494. ocfs2_inode_unlock(dir, 1);
  1495. brelse(new_fe_bh);
  1496. brelse(parent_fe_bh);
  1497. brelse(de_bh);
  1498. kfree(si.name);
  1499. kfree(si.value);
  1500. if (inode_ac)
  1501. ocfs2_free_alloc_context(inode_ac);
  1502. if (data_ac)
  1503. ocfs2_free_alloc_context(data_ac);
  1504. if (xattr_ac)
  1505. ocfs2_free_alloc_context(xattr_ac);
  1506. if ((status < 0) && inode) {
  1507. clear_nlink(inode);
  1508. iput(inode);
  1509. }
  1510. mlog_exit(status);
  1511. return status;
  1512. }
  1513. static int ocfs2_blkno_stringify(u64 blkno, char *name)
  1514. {
  1515. int status, namelen;
  1516. mlog_entry_void();
  1517. namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
  1518. (long long)blkno);
  1519. if (namelen <= 0) {
  1520. if (namelen)
  1521. status = namelen;
  1522. else
  1523. status = -EINVAL;
  1524. mlog_errno(status);
  1525. goto bail;
  1526. }
  1527. if (namelen != OCFS2_ORPHAN_NAMELEN) {
  1528. status = -EINVAL;
  1529. mlog_errno(status);
  1530. goto bail;
  1531. }
  1532. mlog(0, "built filename '%s' for orphan dir (len=%d)\n", name,
  1533. namelen);
  1534. status = 0;
  1535. bail:
  1536. mlog_exit(status);
  1537. return status;
  1538. }
  1539. static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
  1540. struct inode **ret_orphan_dir,
  1541. struct inode *inode,
  1542. char *name,
  1543. struct buffer_head **de_bh)
  1544. {
  1545. struct inode *orphan_dir_inode;
  1546. struct buffer_head *orphan_dir_bh = NULL;
  1547. int status = 0;
  1548. status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
  1549. if (status < 0) {
  1550. mlog_errno(status);
  1551. return status;
  1552. }
  1553. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  1554. ORPHAN_DIR_SYSTEM_INODE,
  1555. osb->slot_num);
  1556. if (!orphan_dir_inode) {
  1557. status = -ENOENT;
  1558. mlog_errno(status);
  1559. return status;
  1560. }
  1561. mutex_lock(&orphan_dir_inode->i_mutex);
  1562. status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
  1563. if (status < 0) {
  1564. mlog_errno(status);
  1565. goto leave;
  1566. }
  1567. status = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
  1568. orphan_dir_bh, name,
  1569. OCFS2_ORPHAN_NAMELEN, de_bh);
  1570. if (status < 0) {
  1571. ocfs2_inode_unlock(orphan_dir_inode, 1);
  1572. mlog_errno(status);
  1573. goto leave;
  1574. }
  1575. *ret_orphan_dir = orphan_dir_inode;
  1576. leave:
  1577. if (status) {
  1578. mutex_unlock(&orphan_dir_inode->i_mutex);
  1579. iput(orphan_dir_inode);
  1580. }
  1581. brelse(orphan_dir_bh);
  1582. mlog_exit(status);
  1583. return status;
  1584. }
  1585. static int ocfs2_orphan_add(struct ocfs2_super *osb,
  1586. handle_t *handle,
  1587. struct inode *inode,
  1588. struct ocfs2_dinode *fe,
  1589. char *name,
  1590. struct buffer_head *de_bh,
  1591. struct inode *orphan_dir_inode)
  1592. {
  1593. struct buffer_head *orphan_dir_bh = NULL;
  1594. int status = 0;
  1595. struct ocfs2_dinode *orphan_fe;
  1596. mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
  1597. status = ocfs2_read_block(orphan_dir_inode,
  1598. OCFS2_I(orphan_dir_inode)->ip_blkno,
  1599. &orphan_dir_bh);
  1600. if (status < 0) {
  1601. mlog_errno(status);
  1602. goto leave;
  1603. }
  1604. status = ocfs2_journal_access(handle, orphan_dir_inode, orphan_dir_bh,
  1605. OCFS2_JOURNAL_ACCESS_WRITE);
  1606. if (status < 0) {
  1607. mlog_errno(status);
  1608. goto leave;
  1609. }
  1610. /* we're a cluster, and nlink can change on disk from
  1611. * underneath us... */
  1612. orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
  1613. if (S_ISDIR(inode->i_mode))
  1614. le16_add_cpu(&orphan_fe->i_links_count, 1);
  1615. orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
  1616. status = ocfs2_journal_dirty(handle, orphan_dir_bh);
  1617. if (status < 0) {
  1618. mlog_errno(status);
  1619. goto leave;
  1620. }
  1621. status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
  1622. OCFS2_ORPHAN_NAMELEN, inode,
  1623. OCFS2_I(inode)->ip_blkno,
  1624. orphan_dir_bh, de_bh);
  1625. if (status < 0) {
  1626. mlog_errno(status);
  1627. goto leave;
  1628. }
  1629. le32_add_cpu(&fe->i_flags, OCFS2_ORPHANED_FL);
  1630. /* Record which orphan dir our inode now resides
  1631. * in. delete_inode will use this to determine which orphan
  1632. * dir to lock. */
  1633. fe->i_orphaned_slot = cpu_to_le16(osb->slot_num);
  1634. mlog(0, "Inode %llu orphaned in slot %d\n",
  1635. (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num);
  1636. leave:
  1637. brelse(orphan_dir_bh);
  1638. mlog_exit(status);
  1639. return status;
  1640. }
  1641. /* unlike orphan_add, we expect the orphan dir to already be locked here. */
  1642. int ocfs2_orphan_del(struct ocfs2_super *osb,
  1643. handle_t *handle,
  1644. struct inode *orphan_dir_inode,
  1645. struct inode *inode,
  1646. struct buffer_head *orphan_dir_bh)
  1647. {
  1648. char name[OCFS2_ORPHAN_NAMELEN + 1];
  1649. struct ocfs2_dinode *orphan_fe;
  1650. int status = 0;
  1651. struct buffer_head *target_de_bh = NULL;
  1652. struct ocfs2_dir_entry *target_de = NULL;
  1653. mlog_entry_void();
  1654. status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
  1655. if (status < 0) {
  1656. mlog_errno(status);
  1657. goto leave;
  1658. }
  1659. mlog(0, "removing '%s' from orphan dir %llu (namelen=%d)\n",
  1660. name, (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
  1661. OCFS2_ORPHAN_NAMELEN);
  1662. /* find it's spot in the orphan directory */
  1663. target_de_bh = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN,
  1664. orphan_dir_inode, &target_de);
  1665. if (!target_de_bh) {
  1666. status = -ENOENT;
  1667. mlog_errno(status);
  1668. goto leave;
  1669. }
  1670. /* remove it from the orphan directory */
  1671. status = ocfs2_delete_entry(handle, orphan_dir_inode, target_de,
  1672. target_de_bh);
  1673. if (status < 0) {
  1674. mlog_errno(status);
  1675. goto leave;
  1676. }
  1677. status = ocfs2_journal_access(handle,orphan_dir_inode, orphan_dir_bh,
  1678. OCFS2_JOURNAL_ACCESS_WRITE);
  1679. if (status < 0) {
  1680. mlog_errno(status);
  1681. goto leave;
  1682. }
  1683. /* do the i_nlink dance! :) */
  1684. orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
  1685. if (S_ISDIR(inode->i_mode))
  1686. le16_add_cpu(&orphan_fe->i_links_count, -1);
  1687. orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
  1688. status = ocfs2_journal_dirty(handle, orphan_dir_bh);
  1689. if (status < 0) {
  1690. mlog_errno(status);
  1691. goto leave;
  1692. }
  1693. leave:
  1694. brelse(target_de_bh);
  1695. mlog_exit(status);
  1696. return status;
  1697. }
  1698. const struct inode_operations ocfs2_dir_iops = {
  1699. .create = ocfs2_create,
  1700. .lookup = ocfs2_lookup,
  1701. .link = ocfs2_link,
  1702. .unlink = ocfs2_unlink,
  1703. .rmdir = ocfs2_unlink,
  1704. .symlink = ocfs2_symlink,
  1705. .mkdir = ocfs2_mkdir,
  1706. .mknod = ocfs2_mknod,
  1707. .rename = ocfs2_rename,
  1708. .setattr = ocfs2_setattr,
  1709. .getattr = ocfs2_getattr,
  1710. .permission = ocfs2_permission,
  1711. .setxattr = generic_setxattr,
  1712. .getxattr = generic_getxattr,
  1713. .listxattr = ocfs2_listxattr,
  1714. .removexattr = generic_removexattr,
  1715. };