namei.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528
  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. #include <linux/quotaops.h>
  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 "ocfs2_trace.h"
  62. #include "buffer_head_io.h"
  63. static int ocfs2_mknod_locked(struct ocfs2_super *osb,
  64. struct inode *dir,
  65. struct inode *inode,
  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. u64 blkno,
  74. char *name,
  75. struct ocfs2_dir_lookup_result *lookup);
  76. static int ocfs2_orphan_add(struct ocfs2_super *osb,
  77. handle_t *handle,
  78. struct inode *inode,
  79. struct buffer_head *fe_bh,
  80. char *name,
  81. struct ocfs2_dir_lookup_result *lookup,
  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. unsigned int flags)
  91. {
  92. int status;
  93. u64 blkno;
  94. struct inode *inode = NULL;
  95. struct dentry *ret;
  96. struct ocfs2_inode_info *oi;
  97. trace_ocfs2_lookup(dir, dentry, dentry->d_name.len,
  98. dentry->d_name.name,
  99. (unsigned long long)OCFS2_I(dir)->ip_blkno, 0);
  100. if (dentry->d_name.len > OCFS2_MAX_FILENAME_LEN) {
  101. ret = ERR_PTR(-ENAMETOOLONG);
  102. goto bail;
  103. }
  104. status = ocfs2_inode_lock_nested(dir, NULL, 0, OI_LS_PARENT);
  105. if (status < 0) {
  106. if (status != -ENOENT)
  107. mlog_errno(status);
  108. ret = ERR_PTR(status);
  109. goto bail;
  110. }
  111. status = ocfs2_lookup_ino_from_name(dir, dentry->d_name.name,
  112. dentry->d_name.len, &blkno);
  113. if (status < 0)
  114. goto bail_add;
  115. inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0, 0);
  116. if (IS_ERR(inode)) {
  117. ret = ERR_PTR(-EACCES);
  118. goto bail_unlock;
  119. }
  120. oi = OCFS2_I(inode);
  121. /* Clear any orphaned state... If we were able to look up the
  122. * inode from a directory, it certainly can't be orphaned. We
  123. * might have the bad state from a node which intended to
  124. * orphan this inode but crashed before it could commit the
  125. * unlink. */
  126. spin_lock(&oi->ip_lock);
  127. oi->ip_flags &= ~OCFS2_INODE_MAYBE_ORPHANED;
  128. spin_unlock(&oi->ip_lock);
  129. bail_add:
  130. ret = d_splice_alias(inode, dentry);
  131. if (inode) {
  132. /*
  133. * If d_splice_alias() finds a DCACHE_DISCONNECTED
  134. * dentry, it will d_move() it on top of ourse. The
  135. * return value will indicate this however, so in
  136. * those cases, we switch them around for the locking
  137. * code.
  138. *
  139. * NOTE: This dentry already has ->d_op set from
  140. * ocfs2_get_parent() and ocfs2_get_dentry()
  141. */
  142. if (ret)
  143. dentry = ret;
  144. status = ocfs2_dentry_attach_lock(dentry, inode,
  145. OCFS2_I(dir)->ip_blkno);
  146. if (status) {
  147. mlog_errno(status);
  148. ret = ERR_PTR(status);
  149. goto bail_unlock;
  150. }
  151. } else
  152. ocfs2_dentry_attach_gen(dentry);
  153. bail_unlock:
  154. /* Don't drop the cluster lock until *after* the d_add --
  155. * unlink on another node will message us to remove that
  156. * dentry under this lock so otherwise we can race this with
  157. * the downconvert thread and have a stale dentry. */
  158. ocfs2_inode_unlock(dir, 0);
  159. bail:
  160. trace_ocfs2_lookup_ret(ret);
  161. return ret;
  162. }
  163. static struct inode *ocfs2_get_init_inode(struct inode *dir, umode_t mode)
  164. {
  165. struct inode *inode;
  166. inode = new_inode(dir->i_sb);
  167. if (!inode) {
  168. mlog(ML_ERROR, "new_inode failed!\n");
  169. return NULL;
  170. }
  171. /* populate as many fields early on as possible - many of
  172. * these are used by the support functions here and in
  173. * callers. */
  174. if (S_ISDIR(mode))
  175. set_nlink(inode, 2);
  176. inode_init_owner(inode, dir, mode);
  177. dquot_initialize(inode);
  178. return inode;
  179. }
  180. static int ocfs2_mknod(struct inode *dir,
  181. struct dentry *dentry,
  182. umode_t mode,
  183. dev_t dev)
  184. {
  185. int status = 0;
  186. struct buffer_head *parent_fe_bh = NULL;
  187. handle_t *handle = NULL;
  188. struct ocfs2_super *osb;
  189. struct ocfs2_dinode *dirfe;
  190. struct buffer_head *new_fe_bh = NULL;
  191. struct inode *inode = NULL;
  192. struct ocfs2_alloc_context *inode_ac = NULL;
  193. struct ocfs2_alloc_context *data_ac = NULL;
  194. struct ocfs2_alloc_context *meta_ac = NULL;
  195. int want_clusters = 0;
  196. int want_meta = 0;
  197. int xattr_credits = 0;
  198. struct ocfs2_security_xattr_info si = {
  199. .enable = 1,
  200. };
  201. int did_quota_inode = 0;
  202. struct ocfs2_dir_lookup_result lookup = { NULL, };
  203. sigset_t oldset;
  204. int did_block_signals = 0;
  205. struct posix_acl *default_acl = NULL, *acl = NULL;
  206. trace_ocfs2_mknod(dir, dentry, dentry->d_name.len, dentry->d_name.name,
  207. (unsigned long long)OCFS2_I(dir)->ip_blkno,
  208. (unsigned long)dev, mode);
  209. dquot_initialize(dir);
  210. /* get our super block */
  211. osb = OCFS2_SB(dir->i_sb);
  212. status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
  213. if (status < 0) {
  214. if (status != -ENOENT)
  215. mlog_errno(status);
  216. return status;
  217. }
  218. if (S_ISDIR(mode) && (dir->i_nlink >= ocfs2_link_max(osb))) {
  219. status = -EMLINK;
  220. goto leave;
  221. }
  222. dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
  223. if (!ocfs2_read_links_count(dirfe)) {
  224. /* can't make a file in a deleted directory. */
  225. status = -ENOENT;
  226. goto leave;
  227. }
  228. status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
  229. dentry->d_name.len);
  230. if (status)
  231. goto leave;
  232. /* get a spot inside the dir. */
  233. status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
  234. dentry->d_name.name,
  235. dentry->d_name.len, &lookup);
  236. if (status < 0) {
  237. mlog_errno(status);
  238. goto leave;
  239. }
  240. /* reserve an inode spot */
  241. status = ocfs2_reserve_new_inode(osb, &inode_ac);
  242. if (status < 0) {
  243. if (status != -ENOSPC)
  244. mlog_errno(status);
  245. goto leave;
  246. }
  247. inode = ocfs2_get_init_inode(dir, mode);
  248. if (!inode) {
  249. status = -ENOMEM;
  250. mlog_errno(status);
  251. goto leave;
  252. }
  253. /* get security xattr */
  254. status = ocfs2_init_security_get(inode, dir, &dentry->d_name, &si);
  255. if (status) {
  256. if (status == -EOPNOTSUPP)
  257. si.enable = 0;
  258. else {
  259. mlog_errno(status);
  260. goto leave;
  261. }
  262. }
  263. /* calculate meta data/clusters for setting security and acl xattr */
  264. status = ocfs2_calc_xattr_init(dir, parent_fe_bh, mode,
  265. &si, &want_clusters,
  266. &xattr_credits, &want_meta);
  267. if (status < 0) {
  268. mlog_errno(status);
  269. goto leave;
  270. }
  271. /* Reserve a cluster if creating an extent based directory. */
  272. if (S_ISDIR(mode) && !ocfs2_supports_inline_data(osb)) {
  273. want_clusters += 1;
  274. /* Dir indexing requires extra space as well */
  275. if (ocfs2_supports_indexed_dirs(osb))
  276. want_meta++;
  277. }
  278. status = ocfs2_reserve_new_metadata_blocks(osb, want_meta, &meta_ac);
  279. if (status < 0) {
  280. if (status != -ENOSPC)
  281. mlog_errno(status);
  282. goto leave;
  283. }
  284. status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac);
  285. if (status < 0) {
  286. if (status != -ENOSPC)
  287. mlog_errno(status);
  288. goto leave;
  289. }
  290. status = posix_acl_create(dir, &mode, &default_acl, &acl);
  291. if (status) {
  292. mlog_errno(status);
  293. goto leave;
  294. }
  295. handle = ocfs2_start_trans(osb, ocfs2_mknod_credits(osb->sb,
  296. S_ISDIR(mode),
  297. xattr_credits));
  298. if (IS_ERR(handle)) {
  299. status = PTR_ERR(handle);
  300. handle = NULL;
  301. mlog_errno(status);
  302. goto leave;
  303. }
  304. /* Starting to change things, restart is no longer possible. */
  305. ocfs2_block_signals(&oldset);
  306. did_block_signals = 1;
  307. status = dquot_alloc_inode(inode);
  308. if (status)
  309. goto leave;
  310. did_quota_inode = 1;
  311. /* do the real work now. */
  312. status = ocfs2_mknod_locked(osb, dir, inode, dev,
  313. &new_fe_bh, parent_fe_bh, handle,
  314. inode_ac);
  315. if (status < 0) {
  316. mlog_errno(status);
  317. goto leave;
  318. }
  319. if (S_ISDIR(mode)) {
  320. status = ocfs2_fill_new_dir(osb, handle, dir, inode,
  321. new_fe_bh, data_ac, meta_ac);
  322. if (status < 0) {
  323. mlog_errno(status);
  324. goto leave;
  325. }
  326. status = ocfs2_journal_access_di(handle, INODE_CACHE(dir),
  327. parent_fe_bh,
  328. OCFS2_JOURNAL_ACCESS_WRITE);
  329. if (status < 0) {
  330. mlog_errno(status);
  331. goto leave;
  332. }
  333. ocfs2_add_links_count(dirfe, 1);
  334. ocfs2_journal_dirty(handle, parent_fe_bh);
  335. inc_nlink(dir);
  336. }
  337. if (default_acl) {
  338. status = ocfs2_set_acl(handle, inode, new_fe_bh,
  339. ACL_TYPE_DEFAULT, default_acl,
  340. meta_ac, data_ac);
  341. }
  342. if (!status && acl) {
  343. status = ocfs2_set_acl(handle, inode, new_fe_bh,
  344. ACL_TYPE_ACCESS, acl,
  345. meta_ac, data_ac);
  346. }
  347. if (status < 0) {
  348. mlog_errno(status);
  349. goto leave;
  350. }
  351. if (si.enable) {
  352. status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si,
  353. meta_ac, data_ac);
  354. if (status < 0) {
  355. mlog_errno(status);
  356. goto leave;
  357. }
  358. }
  359. /*
  360. * Do this before adding the entry to the directory. We add
  361. * also set d_op after success so that ->d_iput() will cleanup
  362. * the dentry lock even if ocfs2_add_entry() fails below.
  363. */
  364. status = ocfs2_dentry_attach_lock(dentry, inode,
  365. OCFS2_I(dir)->ip_blkno);
  366. if (status) {
  367. mlog_errno(status);
  368. goto leave;
  369. }
  370. status = ocfs2_add_entry(handle, dentry, inode,
  371. OCFS2_I(inode)->ip_blkno, parent_fe_bh,
  372. &lookup);
  373. if (status < 0) {
  374. mlog_errno(status);
  375. goto leave;
  376. }
  377. insert_inode_hash(inode);
  378. d_instantiate(dentry, inode);
  379. status = 0;
  380. leave:
  381. if (default_acl)
  382. posix_acl_release(default_acl);
  383. if (acl)
  384. posix_acl_release(acl);
  385. if (status < 0 && did_quota_inode)
  386. dquot_free_inode(inode);
  387. if (handle)
  388. ocfs2_commit_trans(osb, handle);
  389. ocfs2_inode_unlock(dir, 1);
  390. if (did_block_signals)
  391. ocfs2_unblock_signals(&oldset);
  392. brelse(new_fe_bh);
  393. brelse(parent_fe_bh);
  394. kfree(si.name);
  395. kfree(si.value);
  396. ocfs2_free_dir_lookup_result(&lookup);
  397. if (inode_ac)
  398. ocfs2_free_alloc_context(inode_ac);
  399. if (data_ac)
  400. ocfs2_free_alloc_context(data_ac);
  401. if (meta_ac)
  402. ocfs2_free_alloc_context(meta_ac);
  403. /*
  404. * We should call iput after the i_mutex of the bitmap been
  405. * unlocked in ocfs2_free_alloc_context, or the
  406. * ocfs2_delete_inode will mutex_lock again.
  407. */
  408. if ((status < 0) && inode) {
  409. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SKIP_ORPHAN_DIR;
  410. clear_nlink(inode);
  411. iput(inode);
  412. }
  413. if (status)
  414. mlog_errno(status);
  415. return status;
  416. }
  417. static int __ocfs2_mknod_locked(struct inode *dir,
  418. struct inode *inode,
  419. dev_t dev,
  420. struct buffer_head **new_fe_bh,
  421. struct buffer_head *parent_fe_bh,
  422. handle_t *handle,
  423. struct ocfs2_alloc_context *inode_ac,
  424. u64 fe_blkno, u64 suballoc_loc, u16 suballoc_bit)
  425. {
  426. int status = 0;
  427. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  428. struct ocfs2_dinode *fe = NULL;
  429. struct ocfs2_extent_list *fel;
  430. u16 feat;
  431. *new_fe_bh = NULL;
  432. /* populate as many fields early on as possible - many of
  433. * these are used by the support functions here and in
  434. * callers. */
  435. inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
  436. OCFS2_I(inode)->ip_blkno = fe_blkno;
  437. spin_lock(&osb->osb_lock);
  438. inode->i_generation = osb->s_next_generation++;
  439. spin_unlock(&osb->osb_lock);
  440. *new_fe_bh = sb_getblk(osb->sb, fe_blkno);
  441. if (!*new_fe_bh) {
  442. status = -ENOMEM;
  443. mlog_errno(status);
  444. goto leave;
  445. }
  446. ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), *new_fe_bh);
  447. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
  448. *new_fe_bh,
  449. OCFS2_JOURNAL_ACCESS_CREATE);
  450. if (status < 0) {
  451. mlog_errno(status);
  452. goto leave;
  453. }
  454. fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data;
  455. memset(fe, 0, osb->sb->s_blocksize);
  456. fe->i_generation = cpu_to_le32(inode->i_generation);
  457. fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
  458. fe->i_blkno = cpu_to_le64(fe_blkno);
  459. fe->i_suballoc_loc = cpu_to_le64(suballoc_loc);
  460. fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
  461. fe->i_suballoc_slot = cpu_to_le16(inode_ac->ac_alloc_slot);
  462. fe->i_uid = cpu_to_le32(i_uid_read(inode));
  463. fe->i_gid = cpu_to_le32(i_gid_read(inode));
  464. fe->i_mode = cpu_to_le16(inode->i_mode);
  465. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
  466. fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
  467. ocfs2_set_links_count(fe, inode->i_nlink);
  468. fe->i_last_eb_blk = 0;
  469. strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
  470. fe->i_flags |= cpu_to_le32(OCFS2_VALID_FL);
  471. fe->i_atime = fe->i_ctime = fe->i_mtime =
  472. cpu_to_le64(CURRENT_TIME.tv_sec);
  473. fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec =
  474. cpu_to_le32(CURRENT_TIME.tv_nsec);
  475. fe->i_dtime = 0;
  476. /*
  477. * If supported, directories start with inline data. If inline
  478. * isn't supported, but indexing is, we start them as indexed.
  479. */
  480. feat = le16_to_cpu(fe->i_dyn_features);
  481. if (S_ISDIR(inode->i_mode) && ocfs2_supports_inline_data(osb)) {
  482. fe->i_dyn_features = cpu_to_le16(feat | OCFS2_INLINE_DATA_FL);
  483. fe->id2.i_data.id_count = cpu_to_le16(
  484. ocfs2_max_inline_data_with_xattr(osb->sb, fe));
  485. } else {
  486. fel = &fe->id2.i_list;
  487. fel->l_tree_depth = 0;
  488. fel->l_next_free_rec = 0;
  489. fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb));
  490. }
  491. ocfs2_journal_dirty(handle, *new_fe_bh);
  492. ocfs2_populate_inode(inode, fe, 1);
  493. ocfs2_ci_set_new(osb, INODE_CACHE(inode));
  494. if (!ocfs2_mount_local(osb)) {
  495. status = ocfs2_create_new_inode_locks(inode);
  496. if (status < 0)
  497. mlog_errno(status);
  498. }
  499. status = 0; /* error in ocfs2_create_new_inode_locks is not
  500. * critical */
  501. leave:
  502. if (status < 0) {
  503. if (*new_fe_bh) {
  504. brelse(*new_fe_bh);
  505. *new_fe_bh = NULL;
  506. }
  507. }
  508. if (status)
  509. mlog_errno(status);
  510. return status;
  511. }
  512. static int ocfs2_mknod_locked(struct ocfs2_super *osb,
  513. struct inode *dir,
  514. struct inode *inode,
  515. dev_t dev,
  516. struct buffer_head **new_fe_bh,
  517. struct buffer_head *parent_fe_bh,
  518. handle_t *handle,
  519. struct ocfs2_alloc_context *inode_ac)
  520. {
  521. int status = 0;
  522. u64 suballoc_loc, fe_blkno = 0;
  523. u16 suballoc_bit;
  524. *new_fe_bh = NULL;
  525. status = ocfs2_claim_new_inode(handle, dir, parent_fe_bh,
  526. inode_ac, &suballoc_loc,
  527. &suballoc_bit, &fe_blkno);
  528. if (status < 0) {
  529. mlog_errno(status);
  530. return status;
  531. }
  532. return __ocfs2_mknod_locked(dir, inode, dev, new_fe_bh,
  533. parent_fe_bh, handle, inode_ac,
  534. fe_blkno, suballoc_loc, suballoc_bit);
  535. }
  536. static int ocfs2_mkdir(struct inode *dir,
  537. struct dentry *dentry,
  538. umode_t mode)
  539. {
  540. int ret;
  541. trace_ocfs2_mkdir(dir, dentry, dentry->d_name.len, dentry->d_name.name,
  542. OCFS2_I(dir)->ip_blkno, mode);
  543. ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
  544. if (ret)
  545. mlog_errno(ret);
  546. return ret;
  547. }
  548. static int ocfs2_create(struct inode *dir,
  549. struct dentry *dentry,
  550. umode_t mode,
  551. bool excl)
  552. {
  553. int ret;
  554. trace_ocfs2_create(dir, dentry, dentry->d_name.len, dentry->d_name.name,
  555. (unsigned long long)OCFS2_I(dir)->ip_blkno, mode);
  556. ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0);
  557. if (ret)
  558. mlog_errno(ret);
  559. return ret;
  560. }
  561. static int ocfs2_link(struct dentry *old_dentry,
  562. struct inode *dir,
  563. struct dentry *dentry)
  564. {
  565. handle_t *handle;
  566. struct inode *inode = old_dentry->d_inode;
  567. int err;
  568. struct buffer_head *fe_bh = NULL;
  569. struct buffer_head *parent_fe_bh = NULL;
  570. struct ocfs2_dinode *fe = NULL;
  571. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  572. struct ocfs2_dir_lookup_result lookup = { NULL, };
  573. sigset_t oldset;
  574. trace_ocfs2_link((unsigned long long)OCFS2_I(inode)->ip_blkno,
  575. old_dentry->d_name.len, old_dentry->d_name.name,
  576. dentry->d_name.len, dentry->d_name.name);
  577. if (S_ISDIR(inode->i_mode))
  578. return -EPERM;
  579. dquot_initialize(dir);
  580. err = ocfs2_inode_lock_nested(dir, &parent_fe_bh, 1, OI_LS_PARENT);
  581. if (err < 0) {
  582. if (err != -ENOENT)
  583. mlog_errno(err);
  584. return err;
  585. }
  586. if (!dir->i_nlink) {
  587. err = -ENOENT;
  588. goto out;
  589. }
  590. err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
  591. dentry->d_name.len);
  592. if (err)
  593. goto out;
  594. err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
  595. dentry->d_name.name,
  596. dentry->d_name.len, &lookup);
  597. if (err < 0) {
  598. mlog_errno(err);
  599. goto out;
  600. }
  601. err = ocfs2_inode_lock(inode, &fe_bh, 1);
  602. if (err < 0) {
  603. if (err != -ENOENT)
  604. mlog_errno(err);
  605. goto out;
  606. }
  607. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  608. if (ocfs2_read_links_count(fe) >= ocfs2_link_max(osb)) {
  609. err = -EMLINK;
  610. goto out_unlock_inode;
  611. }
  612. handle = ocfs2_start_trans(osb, ocfs2_link_credits(osb->sb));
  613. if (IS_ERR(handle)) {
  614. err = PTR_ERR(handle);
  615. handle = NULL;
  616. mlog_errno(err);
  617. goto out_unlock_inode;
  618. }
  619. /* Starting to change things, restart is no longer possible. */
  620. ocfs2_block_signals(&oldset);
  621. err = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
  622. OCFS2_JOURNAL_ACCESS_WRITE);
  623. if (err < 0) {
  624. mlog_errno(err);
  625. goto out_commit;
  626. }
  627. inc_nlink(inode);
  628. inode->i_ctime = CURRENT_TIME;
  629. ocfs2_set_links_count(fe, inode->i_nlink);
  630. fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  631. fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  632. ocfs2_journal_dirty(handle, fe_bh);
  633. err = ocfs2_add_entry(handle, dentry, inode,
  634. OCFS2_I(inode)->ip_blkno,
  635. parent_fe_bh, &lookup);
  636. if (err) {
  637. ocfs2_add_links_count(fe, -1);
  638. drop_nlink(inode);
  639. mlog_errno(err);
  640. goto out_commit;
  641. }
  642. err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
  643. if (err) {
  644. mlog_errno(err);
  645. goto out_commit;
  646. }
  647. ihold(inode);
  648. d_instantiate(dentry, inode);
  649. out_commit:
  650. ocfs2_commit_trans(osb, handle);
  651. ocfs2_unblock_signals(&oldset);
  652. out_unlock_inode:
  653. ocfs2_inode_unlock(inode, 1);
  654. out:
  655. ocfs2_inode_unlock(dir, 1);
  656. brelse(fe_bh);
  657. brelse(parent_fe_bh);
  658. ocfs2_free_dir_lookup_result(&lookup);
  659. if (err)
  660. mlog_errno(err);
  661. return err;
  662. }
  663. /*
  664. * Takes and drops an exclusive lock on the given dentry. This will
  665. * force other nodes to drop it.
  666. */
  667. static int ocfs2_remote_dentry_delete(struct dentry *dentry)
  668. {
  669. int ret;
  670. ret = ocfs2_dentry_lock(dentry, 1);
  671. if (ret)
  672. mlog_errno(ret);
  673. else
  674. ocfs2_dentry_unlock(dentry, 1);
  675. return ret;
  676. }
  677. static inline int ocfs2_inode_is_unlinkable(struct inode *inode)
  678. {
  679. if (S_ISDIR(inode->i_mode)) {
  680. if (inode->i_nlink == 2)
  681. return 1;
  682. return 0;
  683. }
  684. if (inode->i_nlink == 1)
  685. return 1;
  686. return 0;
  687. }
  688. static int ocfs2_unlink(struct inode *dir,
  689. struct dentry *dentry)
  690. {
  691. int status;
  692. int child_locked = 0;
  693. bool is_unlinkable = false;
  694. struct inode *inode = dentry->d_inode;
  695. struct inode *orphan_dir = NULL;
  696. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  697. u64 blkno;
  698. struct ocfs2_dinode *fe = NULL;
  699. struct buffer_head *fe_bh = NULL;
  700. struct buffer_head *parent_node_bh = NULL;
  701. handle_t *handle = NULL;
  702. char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
  703. struct ocfs2_dir_lookup_result lookup = { NULL, };
  704. struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
  705. trace_ocfs2_unlink(dir, dentry, dentry->d_name.len,
  706. dentry->d_name.name,
  707. (unsigned long long)OCFS2_I(dir)->ip_blkno,
  708. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  709. dquot_initialize(dir);
  710. BUG_ON(dentry->d_parent->d_inode != dir);
  711. if (inode == osb->root_inode)
  712. return -EPERM;
  713. status = ocfs2_inode_lock_nested(dir, &parent_node_bh, 1,
  714. OI_LS_PARENT);
  715. if (status < 0) {
  716. if (status != -ENOENT)
  717. mlog_errno(status);
  718. return status;
  719. }
  720. status = ocfs2_find_files_on_disk(dentry->d_name.name,
  721. dentry->d_name.len, &blkno, dir,
  722. &lookup);
  723. if (status < 0) {
  724. if (status != -ENOENT)
  725. mlog_errno(status);
  726. goto leave;
  727. }
  728. if (OCFS2_I(inode)->ip_blkno != blkno) {
  729. status = -ENOENT;
  730. trace_ocfs2_unlink_noent(
  731. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  732. (unsigned long long)blkno,
  733. OCFS2_I(inode)->ip_flags);
  734. goto leave;
  735. }
  736. status = ocfs2_inode_lock(inode, &fe_bh, 1);
  737. if (status < 0) {
  738. if (status != -ENOENT)
  739. mlog_errno(status);
  740. goto leave;
  741. }
  742. child_locked = 1;
  743. if (S_ISDIR(inode->i_mode)) {
  744. if (inode->i_nlink != 2 || !ocfs2_empty_dir(inode)) {
  745. status = -ENOTEMPTY;
  746. goto leave;
  747. }
  748. }
  749. status = ocfs2_remote_dentry_delete(dentry);
  750. if (status < 0) {
  751. /* This remote delete should succeed under all normal
  752. * circumstances. */
  753. mlog_errno(status);
  754. goto leave;
  755. }
  756. if (ocfs2_inode_is_unlinkable(inode)) {
  757. status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
  758. OCFS2_I(inode)->ip_blkno,
  759. orphan_name, &orphan_insert);
  760. if (status < 0) {
  761. mlog_errno(status);
  762. goto leave;
  763. }
  764. is_unlinkable = true;
  765. }
  766. handle = ocfs2_start_trans(osb, ocfs2_unlink_credits(osb->sb));
  767. if (IS_ERR(handle)) {
  768. status = PTR_ERR(handle);
  769. handle = NULL;
  770. mlog_errno(status);
  771. goto leave;
  772. }
  773. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
  774. OCFS2_JOURNAL_ACCESS_WRITE);
  775. if (status < 0) {
  776. mlog_errno(status);
  777. goto leave;
  778. }
  779. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  780. /* delete the name from the parent dir */
  781. status = ocfs2_delete_entry(handle, dir, &lookup);
  782. if (status < 0) {
  783. mlog_errno(status);
  784. goto leave;
  785. }
  786. if (S_ISDIR(inode->i_mode))
  787. drop_nlink(inode);
  788. drop_nlink(inode);
  789. ocfs2_set_links_count(fe, inode->i_nlink);
  790. ocfs2_journal_dirty(handle, fe_bh);
  791. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  792. if (S_ISDIR(inode->i_mode))
  793. drop_nlink(dir);
  794. status = ocfs2_mark_inode_dirty(handle, dir, parent_node_bh);
  795. if (status < 0) {
  796. mlog_errno(status);
  797. if (S_ISDIR(inode->i_mode))
  798. inc_nlink(dir);
  799. goto leave;
  800. }
  801. if (is_unlinkable) {
  802. status = ocfs2_orphan_add(osb, handle, inode, fe_bh,
  803. orphan_name, &orphan_insert, orphan_dir);
  804. if (status < 0)
  805. mlog_errno(status);
  806. }
  807. leave:
  808. if (handle)
  809. ocfs2_commit_trans(osb, handle);
  810. if (child_locked)
  811. ocfs2_inode_unlock(inode, 1);
  812. ocfs2_inode_unlock(dir, 1);
  813. if (orphan_dir) {
  814. /* This was locked for us in ocfs2_prepare_orphan_dir() */
  815. ocfs2_inode_unlock(orphan_dir, 1);
  816. mutex_unlock(&orphan_dir->i_mutex);
  817. iput(orphan_dir);
  818. }
  819. brelse(fe_bh);
  820. brelse(parent_node_bh);
  821. ocfs2_free_dir_lookup_result(&orphan_insert);
  822. ocfs2_free_dir_lookup_result(&lookup);
  823. if (status && (status != -ENOTEMPTY) && (status != -ENOENT))
  824. mlog_errno(status);
  825. return status;
  826. }
  827. /*
  828. * The only place this should be used is rename!
  829. * if they have the same id, then the 1st one is the only one locked.
  830. */
  831. static int ocfs2_double_lock(struct ocfs2_super *osb,
  832. struct buffer_head **bh1,
  833. struct inode *inode1,
  834. struct buffer_head **bh2,
  835. struct inode *inode2)
  836. {
  837. int status;
  838. struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
  839. struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
  840. struct buffer_head **tmpbh;
  841. struct inode *tmpinode;
  842. trace_ocfs2_double_lock((unsigned long long)oi1->ip_blkno,
  843. (unsigned long long)oi2->ip_blkno);
  844. if (*bh1)
  845. *bh1 = NULL;
  846. if (*bh2)
  847. *bh2 = NULL;
  848. /* we always want to lock the one with the lower lockid first. */
  849. if (oi1->ip_blkno != oi2->ip_blkno) {
  850. if (oi1->ip_blkno < oi2->ip_blkno) {
  851. /* switch id1 and id2 around */
  852. tmpbh = bh2;
  853. bh2 = bh1;
  854. bh1 = tmpbh;
  855. tmpinode = inode2;
  856. inode2 = inode1;
  857. inode1 = tmpinode;
  858. }
  859. /* lock id2 */
  860. status = ocfs2_inode_lock_nested(inode2, bh2, 1,
  861. OI_LS_RENAME1);
  862. if (status < 0) {
  863. if (status != -ENOENT)
  864. mlog_errno(status);
  865. goto bail;
  866. }
  867. }
  868. /* lock id1 */
  869. status = ocfs2_inode_lock_nested(inode1, bh1, 1, OI_LS_RENAME2);
  870. if (status < 0) {
  871. /*
  872. * An error return must mean that no cluster locks
  873. * were held on function exit.
  874. */
  875. if (oi1->ip_blkno != oi2->ip_blkno) {
  876. ocfs2_inode_unlock(inode2, 1);
  877. brelse(*bh2);
  878. *bh2 = NULL;
  879. }
  880. if (status != -ENOENT)
  881. mlog_errno(status);
  882. }
  883. trace_ocfs2_double_lock_end(
  884. (unsigned long long)OCFS2_I(inode1)->ip_blkno,
  885. (unsigned long long)OCFS2_I(inode2)->ip_blkno);
  886. bail:
  887. if (status)
  888. mlog_errno(status);
  889. return status;
  890. }
  891. static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2)
  892. {
  893. ocfs2_inode_unlock(inode1, 1);
  894. if (inode1 != inode2)
  895. ocfs2_inode_unlock(inode2, 1);
  896. }
  897. static int ocfs2_rename(struct inode *old_dir,
  898. struct dentry *old_dentry,
  899. struct inode *new_dir,
  900. struct dentry *new_dentry)
  901. {
  902. int status = 0, rename_lock = 0, parents_locked = 0, target_exists = 0;
  903. int old_child_locked = 0, new_child_locked = 0, update_dot_dot = 0;
  904. struct inode *old_inode = old_dentry->d_inode;
  905. struct inode *new_inode = new_dentry->d_inode;
  906. struct inode *orphan_dir = NULL;
  907. struct ocfs2_dinode *newfe = NULL;
  908. char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
  909. struct buffer_head *newfe_bh = NULL;
  910. struct buffer_head *old_inode_bh = NULL;
  911. struct ocfs2_super *osb = NULL;
  912. u64 newfe_blkno, old_de_ino;
  913. handle_t *handle = NULL;
  914. struct buffer_head *old_dir_bh = NULL;
  915. struct buffer_head *new_dir_bh = NULL;
  916. u32 old_dir_nlink = old_dir->i_nlink;
  917. struct ocfs2_dinode *old_di;
  918. struct ocfs2_dir_lookup_result old_inode_dot_dot_res = { NULL, };
  919. struct ocfs2_dir_lookup_result target_lookup_res = { NULL, };
  920. struct ocfs2_dir_lookup_result old_entry_lookup = { NULL, };
  921. struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
  922. struct ocfs2_dir_lookup_result target_insert = { NULL, };
  923. /* At some point it might be nice to break this function up a
  924. * bit. */
  925. trace_ocfs2_rename(old_dir, old_dentry, new_dir, new_dentry,
  926. old_dentry->d_name.len, old_dentry->d_name.name,
  927. new_dentry->d_name.len, new_dentry->d_name.name);
  928. dquot_initialize(old_dir);
  929. dquot_initialize(new_dir);
  930. osb = OCFS2_SB(old_dir->i_sb);
  931. if (new_inode) {
  932. if (!igrab(new_inode))
  933. BUG();
  934. }
  935. /* Assume a directory hierarchy thusly:
  936. * a/b/c
  937. * a/d
  938. * a,b,c, and d are all directories.
  939. *
  940. * from cwd of 'a' on both nodes:
  941. * node1: mv b/c d
  942. * node2: mv d b/c
  943. *
  944. * And that's why, just like the VFS, we need a file system
  945. * rename lock. */
  946. if (old_dir != new_dir && S_ISDIR(old_inode->i_mode)) {
  947. status = ocfs2_rename_lock(osb);
  948. if (status < 0) {
  949. mlog_errno(status);
  950. goto bail;
  951. }
  952. rename_lock = 1;
  953. }
  954. /* if old and new are the same, this'll just do one lock. */
  955. status = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
  956. &new_dir_bh, new_dir);
  957. if (status < 0) {
  958. mlog_errno(status);
  959. goto bail;
  960. }
  961. parents_locked = 1;
  962. /* make sure both dirs have bhs
  963. * get an extra ref on old_dir_bh if old==new */
  964. if (!new_dir_bh) {
  965. if (old_dir_bh) {
  966. new_dir_bh = old_dir_bh;
  967. get_bh(new_dir_bh);
  968. } else {
  969. mlog(ML_ERROR, "no old_dir_bh!\n");
  970. status = -EIO;
  971. goto bail;
  972. }
  973. }
  974. /*
  975. * Aside from allowing a meta data update, the locking here
  976. * also ensures that the downconvert thread on other nodes
  977. * won't have to concurrently downconvert the inode and the
  978. * dentry locks.
  979. */
  980. status = ocfs2_inode_lock_nested(old_inode, &old_inode_bh, 1,
  981. OI_LS_PARENT);
  982. if (status < 0) {
  983. if (status != -ENOENT)
  984. mlog_errno(status);
  985. goto bail;
  986. }
  987. old_child_locked = 1;
  988. status = ocfs2_remote_dentry_delete(old_dentry);
  989. if (status < 0) {
  990. mlog_errno(status);
  991. goto bail;
  992. }
  993. if (S_ISDIR(old_inode->i_mode)) {
  994. u64 old_inode_parent;
  995. update_dot_dot = 1;
  996. status = ocfs2_find_files_on_disk("..", 2, &old_inode_parent,
  997. old_inode,
  998. &old_inode_dot_dot_res);
  999. if (status) {
  1000. status = -EIO;
  1001. goto bail;
  1002. }
  1003. if (old_inode_parent != OCFS2_I(old_dir)->ip_blkno) {
  1004. status = -EIO;
  1005. goto bail;
  1006. }
  1007. if (!new_inode && new_dir != old_dir &&
  1008. new_dir->i_nlink >= ocfs2_link_max(osb)) {
  1009. status = -EMLINK;
  1010. goto bail;
  1011. }
  1012. }
  1013. status = ocfs2_lookup_ino_from_name(old_dir, old_dentry->d_name.name,
  1014. old_dentry->d_name.len,
  1015. &old_de_ino);
  1016. if (status) {
  1017. status = -ENOENT;
  1018. goto bail;
  1019. }
  1020. /*
  1021. * Check for inode number is _not_ due to possible IO errors.
  1022. * We might rmdir the source, keep it as pwd of some process
  1023. * and merrily kill the link to whatever was created under the
  1024. * same name. Goodbye sticky bit ;-<
  1025. */
  1026. if (old_de_ino != OCFS2_I(old_inode)->ip_blkno) {
  1027. status = -ENOENT;
  1028. goto bail;
  1029. }
  1030. /* check if the target already exists (in which case we need
  1031. * to delete it */
  1032. status = ocfs2_find_files_on_disk(new_dentry->d_name.name,
  1033. new_dentry->d_name.len,
  1034. &newfe_blkno, new_dir,
  1035. &target_lookup_res);
  1036. /* The only error we allow here is -ENOENT because the new
  1037. * file not existing is perfectly valid. */
  1038. if ((status < 0) && (status != -ENOENT)) {
  1039. /* If we cannot find the file specified we should just */
  1040. /* return the error... */
  1041. mlog_errno(status);
  1042. goto bail;
  1043. }
  1044. if (status == 0)
  1045. target_exists = 1;
  1046. if (!target_exists && new_inode) {
  1047. /*
  1048. * Target was unlinked by another node while we were
  1049. * waiting to get to ocfs2_rename(). There isn't
  1050. * anything we can do here to help the situation, so
  1051. * bubble up the appropriate error.
  1052. */
  1053. status = -ENOENT;
  1054. goto bail;
  1055. }
  1056. /* In case we need to overwrite an existing file, we blow it
  1057. * away first */
  1058. if (target_exists) {
  1059. /* VFS didn't think there existed an inode here, but
  1060. * someone else in the cluster must have raced our
  1061. * rename to create one. Today we error cleanly, in
  1062. * the future we should consider calling iget to build
  1063. * a new struct inode for this entry. */
  1064. if (!new_inode) {
  1065. status = -EACCES;
  1066. trace_ocfs2_rename_target_exists(new_dentry->d_name.len,
  1067. new_dentry->d_name.name);
  1068. goto bail;
  1069. }
  1070. if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
  1071. status = -EACCES;
  1072. trace_ocfs2_rename_disagree(
  1073. (unsigned long long)OCFS2_I(new_inode)->ip_blkno,
  1074. (unsigned long long)newfe_blkno,
  1075. OCFS2_I(new_inode)->ip_flags);
  1076. goto bail;
  1077. }
  1078. status = ocfs2_inode_lock(new_inode, &newfe_bh, 1);
  1079. if (status < 0) {
  1080. if (status != -ENOENT)
  1081. mlog_errno(status);
  1082. goto bail;
  1083. }
  1084. new_child_locked = 1;
  1085. status = ocfs2_remote_dentry_delete(new_dentry);
  1086. if (status < 0) {
  1087. mlog_errno(status);
  1088. goto bail;
  1089. }
  1090. newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
  1091. trace_ocfs2_rename_over_existing(
  1092. (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
  1093. (unsigned long long)newfe_bh->b_blocknr : 0ULL);
  1094. if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
  1095. status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
  1096. OCFS2_I(new_inode)->ip_blkno,
  1097. orphan_name, &orphan_insert);
  1098. if (status < 0) {
  1099. mlog_errno(status);
  1100. goto bail;
  1101. }
  1102. }
  1103. } else {
  1104. BUG_ON(new_dentry->d_parent->d_inode != new_dir);
  1105. status = ocfs2_check_dir_for_entry(new_dir,
  1106. new_dentry->d_name.name,
  1107. new_dentry->d_name.len);
  1108. if (status)
  1109. goto bail;
  1110. status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,
  1111. new_dentry->d_name.name,
  1112. new_dentry->d_name.len,
  1113. &target_insert);
  1114. if (status < 0) {
  1115. mlog_errno(status);
  1116. goto bail;
  1117. }
  1118. }
  1119. handle = ocfs2_start_trans(osb, ocfs2_rename_credits(osb->sb));
  1120. if (IS_ERR(handle)) {
  1121. status = PTR_ERR(handle);
  1122. handle = NULL;
  1123. mlog_errno(status);
  1124. goto bail;
  1125. }
  1126. if (target_exists) {
  1127. if (S_ISDIR(new_inode->i_mode)) {
  1128. if (new_inode->i_nlink != 2 ||
  1129. !ocfs2_empty_dir(new_inode)) {
  1130. status = -ENOTEMPTY;
  1131. goto bail;
  1132. }
  1133. }
  1134. status = ocfs2_journal_access_di(handle, INODE_CACHE(new_inode),
  1135. newfe_bh,
  1136. OCFS2_JOURNAL_ACCESS_WRITE);
  1137. if (status < 0) {
  1138. mlog_errno(status);
  1139. goto bail;
  1140. }
  1141. if (S_ISDIR(new_inode->i_mode) ||
  1142. (ocfs2_read_links_count(newfe) == 1)) {
  1143. status = ocfs2_orphan_add(osb, handle, new_inode,
  1144. newfe_bh, orphan_name,
  1145. &orphan_insert, orphan_dir);
  1146. if (status < 0) {
  1147. mlog_errno(status);
  1148. goto bail;
  1149. }
  1150. }
  1151. /* change the dirent to point to the correct inode */
  1152. status = ocfs2_update_entry(new_dir, handle, &target_lookup_res,
  1153. old_inode);
  1154. if (status < 0) {
  1155. mlog_errno(status);
  1156. goto bail;
  1157. }
  1158. new_dir->i_version++;
  1159. if (S_ISDIR(new_inode->i_mode))
  1160. ocfs2_set_links_count(newfe, 0);
  1161. else
  1162. ocfs2_add_links_count(newfe, -1);
  1163. ocfs2_journal_dirty(handle, newfe_bh);
  1164. } else {
  1165. /* if the name was not found in new_dir, add it now */
  1166. status = ocfs2_add_entry(handle, new_dentry, old_inode,
  1167. OCFS2_I(old_inode)->ip_blkno,
  1168. new_dir_bh, &target_insert);
  1169. }
  1170. old_inode->i_ctime = CURRENT_TIME;
  1171. mark_inode_dirty(old_inode);
  1172. status = ocfs2_journal_access_di(handle, INODE_CACHE(old_inode),
  1173. old_inode_bh,
  1174. OCFS2_JOURNAL_ACCESS_WRITE);
  1175. if (status >= 0) {
  1176. old_di = (struct ocfs2_dinode *) old_inode_bh->b_data;
  1177. old_di->i_ctime = cpu_to_le64(old_inode->i_ctime.tv_sec);
  1178. old_di->i_ctime_nsec = cpu_to_le32(old_inode->i_ctime.tv_nsec);
  1179. ocfs2_journal_dirty(handle, old_inode_bh);
  1180. } else
  1181. mlog_errno(status);
  1182. /*
  1183. * Now that the name has been added to new_dir, remove the old name.
  1184. *
  1185. * We don't keep any directory entry context around until now
  1186. * because the insert might have changed the type of directory
  1187. * we're dealing with.
  1188. */
  1189. status = ocfs2_find_entry(old_dentry->d_name.name,
  1190. old_dentry->d_name.len, old_dir,
  1191. &old_entry_lookup);
  1192. if (status)
  1193. goto bail;
  1194. status = ocfs2_delete_entry(handle, old_dir, &old_entry_lookup);
  1195. if (status < 0) {
  1196. mlog_errno(status);
  1197. goto bail;
  1198. }
  1199. if (new_inode) {
  1200. drop_nlink(new_inode);
  1201. new_inode->i_ctime = CURRENT_TIME;
  1202. }
  1203. old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
  1204. if (update_dot_dot) {
  1205. status = ocfs2_update_entry(old_inode, handle,
  1206. &old_inode_dot_dot_res, new_dir);
  1207. drop_nlink(old_dir);
  1208. if (new_inode) {
  1209. drop_nlink(new_inode);
  1210. } else {
  1211. inc_nlink(new_dir);
  1212. mark_inode_dirty(new_dir);
  1213. }
  1214. }
  1215. mark_inode_dirty(old_dir);
  1216. ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh);
  1217. if (new_inode) {
  1218. mark_inode_dirty(new_inode);
  1219. ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh);
  1220. }
  1221. if (old_dir != new_dir) {
  1222. /* Keep the same times on both directories.*/
  1223. new_dir->i_ctime = new_dir->i_mtime = old_dir->i_ctime;
  1224. /*
  1225. * This will also pick up the i_nlink change from the
  1226. * block above.
  1227. */
  1228. ocfs2_mark_inode_dirty(handle, new_dir, new_dir_bh);
  1229. }
  1230. if (old_dir_nlink != old_dir->i_nlink) {
  1231. if (!old_dir_bh) {
  1232. mlog(ML_ERROR, "need to change nlink for old dir "
  1233. "%llu from %d to %d but bh is NULL!\n",
  1234. (unsigned long long)OCFS2_I(old_dir)->ip_blkno,
  1235. (int)old_dir_nlink, old_dir->i_nlink);
  1236. } else {
  1237. struct ocfs2_dinode *fe;
  1238. status = ocfs2_journal_access_di(handle,
  1239. INODE_CACHE(old_dir),
  1240. old_dir_bh,
  1241. OCFS2_JOURNAL_ACCESS_WRITE);
  1242. fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
  1243. ocfs2_set_links_count(fe, old_dir->i_nlink);
  1244. ocfs2_journal_dirty(handle, old_dir_bh);
  1245. }
  1246. }
  1247. ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
  1248. status = 0;
  1249. bail:
  1250. if (rename_lock)
  1251. ocfs2_rename_unlock(osb);
  1252. if (handle)
  1253. ocfs2_commit_trans(osb, handle);
  1254. if (parents_locked)
  1255. ocfs2_double_unlock(old_dir, new_dir);
  1256. if (old_child_locked)
  1257. ocfs2_inode_unlock(old_inode, 1);
  1258. if (new_child_locked)
  1259. ocfs2_inode_unlock(new_inode, 1);
  1260. if (orphan_dir) {
  1261. /* This was locked for us in ocfs2_prepare_orphan_dir() */
  1262. ocfs2_inode_unlock(orphan_dir, 1);
  1263. mutex_unlock(&orphan_dir->i_mutex);
  1264. iput(orphan_dir);
  1265. }
  1266. if (new_inode)
  1267. sync_mapping_buffers(old_inode->i_mapping);
  1268. if (new_inode)
  1269. iput(new_inode);
  1270. ocfs2_free_dir_lookup_result(&target_lookup_res);
  1271. ocfs2_free_dir_lookup_result(&old_entry_lookup);
  1272. ocfs2_free_dir_lookup_result(&old_inode_dot_dot_res);
  1273. ocfs2_free_dir_lookup_result(&orphan_insert);
  1274. ocfs2_free_dir_lookup_result(&target_insert);
  1275. brelse(newfe_bh);
  1276. brelse(old_inode_bh);
  1277. brelse(old_dir_bh);
  1278. brelse(new_dir_bh);
  1279. if (status)
  1280. mlog_errno(status);
  1281. return status;
  1282. }
  1283. /*
  1284. * we expect i_size = strlen(symname). Copy symname into the file
  1285. * data, including the null terminator.
  1286. */
  1287. static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
  1288. handle_t *handle,
  1289. struct inode *inode,
  1290. const char *symname)
  1291. {
  1292. struct buffer_head **bhs = NULL;
  1293. const char *c;
  1294. struct super_block *sb = osb->sb;
  1295. u64 p_blkno, p_blocks;
  1296. int virtual, blocks, status, i, bytes_left;
  1297. bytes_left = i_size_read(inode) + 1;
  1298. /* we can't trust i_blocks because we're actually going to
  1299. * write i_size + 1 bytes. */
  1300. blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
  1301. trace_ocfs2_create_symlink_data((unsigned long long)inode->i_blocks,
  1302. i_size_read(inode), blocks);
  1303. /* Sanity check -- make sure we're going to fit. */
  1304. if (bytes_left >
  1305. ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
  1306. status = -EIO;
  1307. mlog_errno(status);
  1308. goto bail;
  1309. }
  1310. bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
  1311. if (!bhs) {
  1312. status = -ENOMEM;
  1313. mlog_errno(status);
  1314. goto bail;
  1315. }
  1316. status = ocfs2_extent_map_get_blocks(inode, 0, &p_blkno, &p_blocks,
  1317. NULL);
  1318. if (status < 0) {
  1319. mlog_errno(status);
  1320. goto bail;
  1321. }
  1322. /* links can never be larger than one cluster so we know this
  1323. * is all going to be contiguous, but do a sanity check
  1324. * anyway. */
  1325. if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
  1326. status = -EIO;
  1327. mlog_errno(status);
  1328. goto bail;
  1329. }
  1330. virtual = 0;
  1331. while(bytes_left > 0) {
  1332. c = &symname[virtual * sb->s_blocksize];
  1333. bhs[virtual] = sb_getblk(sb, p_blkno);
  1334. if (!bhs[virtual]) {
  1335. status = -ENOMEM;
  1336. mlog_errno(status);
  1337. goto bail;
  1338. }
  1339. ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode),
  1340. bhs[virtual]);
  1341. status = ocfs2_journal_access(handle, INODE_CACHE(inode),
  1342. bhs[virtual],
  1343. OCFS2_JOURNAL_ACCESS_CREATE);
  1344. if (status < 0) {
  1345. mlog_errno(status);
  1346. goto bail;
  1347. }
  1348. memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
  1349. memcpy(bhs[virtual]->b_data, c,
  1350. (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
  1351. bytes_left);
  1352. ocfs2_journal_dirty(handle, bhs[virtual]);
  1353. virtual++;
  1354. p_blkno++;
  1355. bytes_left -= sb->s_blocksize;
  1356. }
  1357. status = 0;
  1358. bail:
  1359. if (bhs) {
  1360. for(i = 0; i < blocks; i++)
  1361. brelse(bhs[i]);
  1362. kfree(bhs);
  1363. }
  1364. if (status)
  1365. mlog_errno(status);
  1366. return status;
  1367. }
  1368. static int ocfs2_symlink(struct inode *dir,
  1369. struct dentry *dentry,
  1370. const char *symname)
  1371. {
  1372. int status, l, credits;
  1373. u64 newsize;
  1374. struct ocfs2_super *osb = NULL;
  1375. struct inode *inode = NULL;
  1376. struct super_block *sb;
  1377. struct buffer_head *new_fe_bh = NULL;
  1378. struct buffer_head *parent_fe_bh = NULL;
  1379. struct ocfs2_dinode *fe = NULL;
  1380. struct ocfs2_dinode *dirfe;
  1381. handle_t *handle = NULL;
  1382. struct ocfs2_alloc_context *inode_ac = NULL;
  1383. struct ocfs2_alloc_context *data_ac = NULL;
  1384. struct ocfs2_alloc_context *xattr_ac = NULL;
  1385. int want_clusters = 0;
  1386. int xattr_credits = 0;
  1387. struct ocfs2_security_xattr_info si = {
  1388. .enable = 1,
  1389. };
  1390. int did_quota = 0, did_quota_inode = 0;
  1391. struct ocfs2_dir_lookup_result lookup = { NULL, };
  1392. sigset_t oldset;
  1393. int did_block_signals = 0;
  1394. trace_ocfs2_symlink_begin(dir, dentry, symname,
  1395. dentry->d_name.len, dentry->d_name.name);
  1396. dquot_initialize(dir);
  1397. sb = dir->i_sb;
  1398. osb = OCFS2_SB(sb);
  1399. l = strlen(symname) + 1;
  1400. credits = ocfs2_calc_symlink_credits(sb);
  1401. /* lock the parent directory */
  1402. status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
  1403. if (status < 0) {
  1404. if (status != -ENOENT)
  1405. mlog_errno(status);
  1406. return status;
  1407. }
  1408. dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
  1409. if (!ocfs2_read_links_count(dirfe)) {
  1410. /* can't make a file in a deleted directory. */
  1411. status = -ENOENT;
  1412. goto bail;
  1413. }
  1414. status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
  1415. dentry->d_name.len);
  1416. if (status)
  1417. goto bail;
  1418. status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
  1419. dentry->d_name.name,
  1420. dentry->d_name.len, &lookup);
  1421. if (status < 0) {
  1422. mlog_errno(status);
  1423. goto bail;
  1424. }
  1425. status = ocfs2_reserve_new_inode(osb, &inode_ac);
  1426. if (status < 0) {
  1427. if (status != -ENOSPC)
  1428. mlog_errno(status);
  1429. goto bail;
  1430. }
  1431. inode = ocfs2_get_init_inode(dir, S_IFLNK | S_IRWXUGO);
  1432. if (!inode) {
  1433. status = -ENOMEM;
  1434. mlog_errno(status);
  1435. goto bail;
  1436. }
  1437. /* get security xattr */
  1438. status = ocfs2_init_security_get(inode, dir, &dentry->d_name, &si);
  1439. if (status) {
  1440. if (status == -EOPNOTSUPP)
  1441. si.enable = 0;
  1442. else {
  1443. mlog_errno(status);
  1444. goto bail;
  1445. }
  1446. }
  1447. /* calculate meta data/clusters for setting security xattr */
  1448. if (si.enable) {
  1449. status = ocfs2_calc_security_init(dir, &si, &want_clusters,
  1450. &xattr_credits, &xattr_ac);
  1451. if (status < 0) {
  1452. mlog_errno(status);
  1453. goto bail;
  1454. }
  1455. }
  1456. /* don't reserve bitmap space for fast symlinks. */
  1457. if (l > ocfs2_fast_symlink_chars(sb))
  1458. want_clusters += 1;
  1459. status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac);
  1460. if (status < 0) {
  1461. if (status != -ENOSPC)
  1462. mlog_errno(status);
  1463. goto bail;
  1464. }
  1465. handle = ocfs2_start_trans(osb, credits + xattr_credits);
  1466. if (IS_ERR(handle)) {
  1467. status = PTR_ERR(handle);
  1468. handle = NULL;
  1469. mlog_errno(status);
  1470. goto bail;
  1471. }
  1472. /* Starting to change things, restart is no longer possible. */
  1473. ocfs2_block_signals(&oldset);
  1474. did_block_signals = 1;
  1475. status = dquot_alloc_inode(inode);
  1476. if (status)
  1477. goto bail;
  1478. did_quota_inode = 1;
  1479. trace_ocfs2_symlink_create(dir, dentry, dentry->d_name.len,
  1480. dentry->d_name.name,
  1481. (unsigned long long)OCFS2_I(dir)->ip_blkno,
  1482. inode->i_mode);
  1483. status = ocfs2_mknod_locked(osb, dir, inode,
  1484. 0, &new_fe_bh, parent_fe_bh, handle,
  1485. inode_ac);
  1486. if (status < 0) {
  1487. mlog_errno(status);
  1488. goto bail;
  1489. }
  1490. fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
  1491. inode->i_rdev = 0;
  1492. newsize = l - 1;
  1493. inode->i_op = &ocfs2_symlink_inode_operations;
  1494. if (l > ocfs2_fast_symlink_chars(sb)) {
  1495. u32 offset = 0;
  1496. status = dquot_alloc_space_nodirty(inode,
  1497. ocfs2_clusters_to_bytes(osb->sb, 1));
  1498. if (status)
  1499. goto bail;
  1500. did_quota = 1;
  1501. inode->i_mapping->a_ops = &ocfs2_aops;
  1502. status = ocfs2_add_inode_data(osb, inode, &offset, 1, 0,
  1503. new_fe_bh,
  1504. handle, data_ac, NULL,
  1505. NULL);
  1506. if (status < 0) {
  1507. if (status != -ENOSPC && status != -EINTR) {
  1508. mlog(ML_ERROR,
  1509. "Failed to extend file to %llu\n",
  1510. (unsigned long long)newsize);
  1511. mlog_errno(status);
  1512. status = -ENOSPC;
  1513. }
  1514. goto bail;
  1515. }
  1516. i_size_write(inode, newsize);
  1517. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1518. } else {
  1519. inode->i_mapping->a_ops = &ocfs2_fast_symlink_aops;
  1520. memcpy((char *) fe->id2.i_symlink, symname, l);
  1521. i_size_write(inode, newsize);
  1522. inode->i_blocks = 0;
  1523. }
  1524. status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
  1525. if (status < 0) {
  1526. mlog_errno(status);
  1527. goto bail;
  1528. }
  1529. if (!ocfs2_inode_is_fast_symlink(inode)) {
  1530. status = ocfs2_create_symlink_data(osb, handle, inode,
  1531. symname);
  1532. if (status < 0) {
  1533. mlog_errno(status);
  1534. goto bail;
  1535. }
  1536. }
  1537. if (si.enable) {
  1538. status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si,
  1539. xattr_ac, data_ac);
  1540. if (status < 0) {
  1541. mlog_errno(status);
  1542. goto bail;
  1543. }
  1544. }
  1545. /*
  1546. * Do this before adding the entry to the directory. We add
  1547. * also set d_op after success so that ->d_iput() will cleanup
  1548. * the dentry lock even if ocfs2_add_entry() fails below.
  1549. */
  1550. status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
  1551. if (status) {
  1552. mlog_errno(status);
  1553. goto bail;
  1554. }
  1555. status = ocfs2_add_entry(handle, dentry, inode,
  1556. le64_to_cpu(fe->i_blkno), parent_fe_bh,
  1557. &lookup);
  1558. if (status < 0) {
  1559. mlog_errno(status);
  1560. goto bail;
  1561. }
  1562. insert_inode_hash(inode);
  1563. d_instantiate(dentry, inode);
  1564. bail:
  1565. if (status < 0 && did_quota)
  1566. dquot_free_space_nodirty(inode,
  1567. ocfs2_clusters_to_bytes(osb->sb, 1));
  1568. if (status < 0 && did_quota_inode)
  1569. dquot_free_inode(inode);
  1570. if (handle)
  1571. ocfs2_commit_trans(osb, handle);
  1572. ocfs2_inode_unlock(dir, 1);
  1573. if (did_block_signals)
  1574. ocfs2_unblock_signals(&oldset);
  1575. brelse(new_fe_bh);
  1576. brelse(parent_fe_bh);
  1577. kfree(si.name);
  1578. kfree(si.value);
  1579. ocfs2_free_dir_lookup_result(&lookup);
  1580. if (inode_ac)
  1581. ocfs2_free_alloc_context(inode_ac);
  1582. if (data_ac)
  1583. ocfs2_free_alloc_context(data_ac);
  1584. if (xattr_ac)
  1585. ocfs2_free_alloc_context(xattr_ac);
  1586. if ((status < 0) && inode) {
  1587. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SKIP_ORPHAN_DIR;
  1588. clear_nlink(inode);
  1589. iput(inode);
  1590. }
  1591. if (status)
  1592. mlog_errno(status);
  1593. return status;
  1594. }
  1595. static int ocfs2_blkno_stringify(u64 blkno, char *name)
  1596. {
  1597. int status, namelen;
  1598. namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
  1599. (long long)blkno);
  1600. if (namelen <= 0) {
  1601. if (namelen)
  1602. status = namelen;
  1603. else
  1604. status = -EINVAL;
  1605. mlog_errno(status);
  1606. goto bail;
  1607. }
  1608. if (namelen != OCFS2_ORPHAN_NAMELEN) {
  1609. status = -EINVAL;
  1610. mlog_errno(status);
  1611. goto bail;
  1612. }
  1613. trace_ocfs2_blkno_stringify(blkno, name, namelen);
  1614. status = 0;
  1615. bail:
  1616. if (status < 0)
  1617. mlog_errno(status);
  1618. return status;
  1619. }
  1620. static int ocfs2_lookup_lock_orphan_dir(struct ocfs2_super *osb,
  1621. struct inode **ret_orphan_dir,
  1622. struct buffer_head **ret_orphan_dir_bh)
  1623. {
  1624. struct inode *orphan_dir_inode;
  1625. struct buffer_head *orphan_dir_bh = NULL;
  1626. int ret = 0;
  1627. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  1628. ORPHAN_DIR_SYSTEM_INODE,
  1629. osb->slot_num);
  1630. if (!orphan_dir_inode) {
  1631. ret = -ENOENT;
  1632. mlog_errno(ret);
  1633. return ret;
  1634. }
  1635. mutex_lock(&orphan_dir_inode->i_mutex);
  1636. ret = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
  1637. if (ret < 0) {
  1638. mutex_unlock(&orphan_dir_inode->i_mutex);
  1639. iput(orphan_dir_inode);
  1640. mlog_errno(ret);
  1641. return ret;
  1642. }
  1643. *ret_orphan_dir = orphan_dir_inode;
  1644. *ret_orphan_dir_bh = orphan_dir_bh;
  1645. return 0;
  1646. }
  1647. static int __ocfs2_prepare_orphan_dir(struct inode *orphan_dir_inode,
  1648. struct buffer_head *orphan_dir_bh,
  1649. u64 blkno,
  1650. char *name,
  1651. struct ocfs2_dir_lookup_result *lookup)
  1652. {
  1653. int ret;
  1654. struct ocfs2_super *osb = OCFS2_SB(orphan_dir_inode->i_sb);
  1655. ret = ocfs2_blkno_stringify(blkno, name);
  1656. if (ret < 0) {
  1657. mlog_errno(ret);
  1658. return ret;
  1659. }
  1660. ret = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
  1661. orphan_dir_bh, name,
  1662. OCFS2_ORPHAN_NAMELEN, lookup);
  1663. if (ret < 0) {
  1664. mlog_errno(ret);
  1665. return ret;
  1666. }
  1667. return 0;
  1668. }
  1669. /**
  1670. * ocfs2_prepare_orphan_dir() - Prepare an orphan directory for
  1671. * insertion of an orphan.
  1672. * @osb: ocfs2 file system
  1673. * @ret_orphan_dir: Orphan dir inode - returned locked!
  1674. * @blkno: Actual block number of the inode to be inserted into orphan dir.
  1675. * @lookup: dir lookup result, to be passed back into functions like
  1676. * ocfs2_orphan_add
  1677. *
  1678. * Returns zero on success and the ret_orphan_dir, name and lookup
  1679. * fields will be populated.
  1680. *
  1681. * Returns non-zero on failure.
  1682. */
  1683. static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
  1684. struct inode **ret_orphan_dir,
  1685. u64 blkno,
  1686. char *name,
  1687. struct ocfs2_dir_lookup_result *lookup)
  1688. {
  1689. struct inode *orphan_dir_inode = NULL;
  1690. struct buffer_head *orphan_dir_bh = NULL;
  1691. int ret = 0;
  1692. ret = ocfs2_lookup_lock_orphan_dir(osb, &orphan_dir_inode,
  1693. &orphan_dir_bh);
  1694. if (ret < 0) {
  1695. mlog_errno(ret);
  1696. return ret;
  1697. }
  1698. ret = __ocfs2_prepare_orphan_dir(orphan_dir_inode, orphan_dir_bh,
  1699. blkno, name, lookup);
  1700. if (ret < 0) {
  1701. mlog_errno(ret);
  1702. goto out;
  1703. }
  1704. *ret_orphan_dir = orphan_dir_inode;
  1705. out:
  1706. brelse(orphan_dir_bh);
  1707. if (ret) {
  1708. ocfs2_inode_unlock(orphan_dir_inode, 1);
  1709. mutex_unlock(&orphan_dir_inode->i_mutex);
  1710. iput(orphan_dir_inode);
  1711. }
  1712. if (ret)
  1713. mlog_errno(ret);
  1714. return ret;
  1715. }
  1716. static int ocfs2_orphan_add(struct ocfs2_super *osb,
  1717. handle_t *handle,
  1718. struct inode *inode,
  1719. struct buffer_head *fe_bh,
  1720. char *name,
  1721. struct ocfs2_dir_lookup_result *lookup,
  1722. struct inode *orphan_dir_inode)
  1723. {
  1724. struct buffer_head *orphan_dir_bh = NULL;
  1725. int status = 0;
  1726. struct ocfs2_dinode *orphan_fe;
  1727. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
  1728. trace_ocfs2_orphan_add_begin(
  1729. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1730. status = ocfs2_read_inode_block(orphan_dir_inode, &orphan_dir_bh);
  1731. if (status < 0) {
  1732. mlog_errno(status);
  1733. goto leave;
  1734. }
  1735. status = ocfs2_journal_access_di(handle,
  1736. INODE_CACHE(orphan_dir_inode),
  1737. orphan_dir_bh,
  1738. OCFS2_JOURNAL_ACCESS_WRITE);
  1739. if (status < 0) {
  1740. mlog_errno(status);
  1741. goto leave;
  1742. }
  1743. /*
  1744. * We're going to journal the change of i_flags and i_orphaned_slot.
  1745. * It's safe anyway, though some callers may duplicate the journaling.
  1746. * Journaling within the func just make the logic look more
  1747. * straightforward.
  1748. */
  1749. status = ocfs2_journal_access_di(handle,
  1750. INODE_CACHE(inode),
  1751. fe_bh,
  1752. OCFS2_JOURNAL_ACCESS_WRITE);
  1753. if (status < 0) {
  1754. mlog_errno(status);
  1755. goto leave;
  1756. }
  1757. /* we're a cluster, and nlink can change on disk from
  1758. * underneath us... */
  1759. orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
  1760. if (S_ISDIR(inode->i_mode))
  1761. ocfs2_add_links_count(orphan_fe, 1);
  1762. set_nlink(orphan_dir_inode, ocfs2_read_links_count(orphan_fe));
  1763. ocfs2_journal_dirty(handle, orphan_dir_bh);
  1764. status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
  1765. OCFS2_ORPHAN_NAMELEN, inode,
  1766. OCFS2_I(inode)->ip_blkno,
  1767. orphan_dir_bh, lookup);
  1768. if (status < 0) {
  1769. mlog_errno(status);
  1770. goto rollback;
  1771. }
  1772. fe->i_flags |= cpu_to_le32(OCFS2_ORPHANED_FL);
  1773. OCFS2_I(inode)->ip_flags &= ~OCFS2_INODE_SKIP_ORPHAN_DIR;
  1774. /* Record which orphan dir our inode now resides
  1775. * in. delete_inode will use this to determine which orphan
  1776. * dir to lock. */
  1777. fe->i_orphaned_slot = cpu_to_le16(osb->slot_num);
  1778. ocfs2_journal_dirty(handle, fe_bh);
  1779. trace_ocfs2_orphan_add_end((unsigned long long)OCFS2_I(inode)->ip_blkno,
  1780. osb->slot_num);
  1781. rollback:
  1782. if (status < 0) {
  1783. if (S_ISDIR(inode->i_mode))
  1784. ocfs2_add_links_count(orphan_fe, -1);
  1785. set_nlink(orphan_dir_inode, ocfs2_read_links_count(orphan_fe));
  1786. }
  1787. leave:
  1788. brelse(orphan_dir_bh);
  1789. return status;
  1790. }
  1791. /* unlike orphan_add, we expect the orphan dir to already be locked here. */
  1792. int ocfs2_orphan_del(struct ocfs2_super *osb,
  1793. handle_t *handle,
  1794. struct inode *orphan_dir_inode,
  1795. struct inode *inode,
  1796. struct buffer_head *orphan_dir_bh)
  1797. {
  1798. char name[OCFS2_ORPHAN_NAMELEN + 1];
  1799. struct ocfs2_dinode *orphan_fe;
  1800. int status = 0;
  1801. struct ocfs2_dir_lookup_result lookup = { NULL, };
  1802. status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
  1803. if (status < 0) {
  1804. mlog_errno(status);
  1805. goto leave;
  1806. }
  1807. trace_ocfs2_orphan_del(
  1808. (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
  1809. name, OCFS2_ORPHAN_NAMELEN);
  1810. /* find it's spot in the orphan directory */
  1811. status = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN, orphan_dir_inode,
  1812. &lookup);
  1813. if (status) {
  1814. mlog_errno(status);
  1815. goto leave;
  1816. }
  1817. /* remove it from the orphan directory */
  1818. status = ocfs2_delete_entry(handle, orphan_dir_inode, &lookup);
  1819. if (status < 0) {
  1820. mlog_errno(status);
  1821. goto leave;
  1822. }
  1823. status = ocfs2_journal_access_di(handle,
  1824. INODE_CACHE(orphan_dir_inode),
  1825. orphan_dir_bh,
  1826. OCFS2_JOURNAL_ACCESS_WRITE);
  1827. if (status < 0) {
  1828. mlog_errno(status);
  1829. goto leave;
  1830. }
  1831. /* do the i_nlink dance! :) */
  1832. orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
  1833. if (S_ISDIR(inode->i_mode))
  1834. ocfs2_add_links_count(orphan_fe, -1);
  1835. set_nlink(orphan_dir_inode, ocfs2_read_links_count(orphan_fe));
  1836. ocfs2_journal_dirty(handle, orphan_dir_bh);
  1837. leave:
  1838. ocfs2_free_dir_lookup_result(&lookup);
  1839. if (status)
  1840. mlog_errno(status);
  1841. return status;
  1842. }
  1843. /**
  1844. * ocfs2_prep_new_orphaned_file() - Prepare the orphan dir to receive a newly
  1845. * allocated file. This is different from the typical 'add to orphan dir'
  1846. * operation in that the inode does not yet exist. This is a problem because
  1847. * the orphan dir stringifies the inode block number to come up with it's
  1848. * dirent. Obviously if the inode does not yet exist we have a chicken and egg
  1849. * problem. This function works around it by calling deeper into the orphan
  1850. * and suballoc code than other callers. Use this only by necessity.
  1851. * @dir: The directory which this inode will ultimately wind up under - not the
  1852. * orphan dir!
  1853. * @dir_bh: buffer_head the @dir inode block
  1854. * @orphan_name: string of length (CFS2_ORPHAN_NAMELEN + 1). Will be filled
  1855. * with the string to be used for orphan dirent. Pass back to the orphan dir
  1856. * code.
  1857. * @ret_orphan_dir: orphan dir inode returned to be passed back into orphan
  1858. * dir code.
  1859. * @ret_di_blkno: block number where the new inode will be allocated.
  1860. * @orphan_insert: Dir insert context to be passed back into orphan dir code.
  1861. * @ret_inode_ac: Inode alloc context to be passed back to the allocator.
  1862. *
  1863. * Returns zero on success and the ret_orphan_dir, name and lookup
  1864. * fields will be populated.
  1865. *
  1866. * Returns non-zero on failure.
  1867. */
  1868. static int ocfs2_prep_new_orphaned_file(struct inode *dir,
  1869. struct buffer_head *dir_bh,
  1870. char *orphan_name,
  1871. struct inode **ret_orphan_dir,
  1872. u64 *ret_di_blkno,
  1873. struct ocfs2_dir_lookup_result *orphan_insert,
  1874. struct ocfs2_alloc_context **ret_inode_ac)
  1875. {
  1876. int ret;
  1877. u64 di_blkno;
  1878. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  1879. struct inode *orphan_dir = NULL;
  1880. struct buffer_head *orphan_dir_bh = NULL;
  1881. struct ocfs2_alloc_context *inode_ac = NULL;
  1882. ret = ocfs2_lookup_lock_orphan_dir(osb, &orphan_dir, &orphan_dir_bh);
  1883. if (ret < 0) {
  1884. mlog_errno(ret);
  1885. return ret;
  1886. }
  1887. /* reserve an inode spot */
  1888. ret = ocfs2_reserve_new_inode(osb, &inode_ac);
  1889. if (ret < 0) {
  1890. if (ret != -ENOSPC)
  1891. mlog_errno(ret);
  1892. goto out;
  1893. }
  1894. ret = ocfs2_find_new_inode_loc(dir, dir_bh, inode_ac,
  1895. &di_blkno);
  1896. if (ret) {
  1897. mlog_errno(ret);
  1898. goto out;
  1899. }
  1900. ret = __ocfs2_prepare_orphan_dir(orphan_dir, orphan_dir_bh,
  1901. di_blkno, orphan_name, orphan_insert);
  1902. if (ret < 0) {
  1903. mlog_errno(ret);
  1904. goto out;
  1905. }
  1906. out:
  1907. if (ret == 0) {
  1908. *ret_orphan_dir = orphan_dir;
  1909. *ret_di_blkno = di_blkno;
  1910. *ret_inode_ac = inode_ac;
  1911. /*
  1912. * orphan_name and orphan_insert are already up to
  1913. * date via prepare_orphan_dir
  1914. */
  1915. } else {
  1916. /* Unroll reserve_new_inode* */
  1917. if (inode_ac)
  1918. ocfs2_free_alloc_context(inode_ac);
  1919. /* Unroll orphan dir locking */
  1920. mutex_unlock(&orphan_dir->i_mutex);
  1921. ocfs2_inode_unlock(orphan_dir, 1);
  1922. iput(orphan_dir);
  1923. }
  1924. brelse(orphan_dir_bh);
  1925. return ret;
  1926. }
  1927. int ocfs2_create_inode_in_orphan(struct inode *dir,
  1928. int mode,
  1929. struct inode **new_inode)
  1930. {
  1931. int status, did_quota_inode = 0;
  1932. struct inode *inode = NULL;
  1933. struct inode *orphan_dir = NULL;
  1934. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  1935. struct ocfs2_dinode *di = NULL;
  1936. handle_t *handle = NULL;
  1937. char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
  1938. struct buffer_head *parent_di_bh = NULL;
  1939. struct buffer_head *new_di_bh = NULL;
  1940. struct ocfs2_alloc_context *inode_ac = NULL;
  1941. struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
  1942. u64 uninitialized_var(di_blkno), suballoc_loc;
  1943. u16 suballoc_bit;
  1944. status = ocfs2_inode_lock(dir, &parent_di_bh, 1);
  1945. if (status < 0) {
  1946. if (status != -ENOENT)
  1947. mlog_errno(status);
  1948. return status;
  1949. }
  1950. status = ocfs2_prep_new_orphaned_file(dir, parent_di_bh,
  1951. orphan_name, &orphan_dir,
  1952. &di_blkno, &orphan_insert, &inode_ac);
  1953. if (status < 0) {
  1954. if (status != -ENOSPC)
  1955. mlog_errno(status);
  1956. goto leave;
  1957. }
  1958. inode = ocfs2_get_init_inode(dir, mode);
  1959. if (!inode) {
  1960. status = -ENOMEM;
  1961. mlog_errno(status);
  1962. goto leave;
  1963. }
  1964. handle = ocfs2_start_trans(osb, ocfs2_mknod_credits(osb->sb, 0, 0));
  1965. if (IS_ERR(handle)) {
  1966. status = PTR_ERR(handle);
  1967. handle = NULL;
  1968. mlog_errno(status);
  1969. goto leave;
  1970. }
  1971. status = dquot_alloc_inode(inode);
  1972. if (status)
  1973. goto leave;
  1974. did_quota_inode = 1;
  1975. status = ocfs2_claim_new_inode_at_loc(handle, dir, inode_ac,
  1976. &suballoc_loc,
  1977. &suballoc_bit, di_blkno);
  1978. if (status < 0) {
  1979. mlog_errno(status);
  1980. goto leave;
  1981. }
  1982. clear_nlink(inode);
  1983. /* do the real work now. */
  1984. status = __ocfs2_mknod_locked(dir, inode,
  1985. 0, &new_di_bh, parent_di_bh, handle,
  1986. inode_ac, di_blkno, suballoc_loc,
  1987. suballoc_bit);
  1988. if (status < 0) {
  1989. mlog_errno(status);
  1990. goto leave;
  1991. }
  1992. di = (struct ocfs2_dinode *)new_di_bh->b_data;
  1993. status = ocfs2_orphan_add(osb, handle, inode, new_di_bh, orphan_name,
  1994. &orphan_insert, orphan_dir);
  1995. if (status < 0) {
  1996. mlog_errno(status);
  1997. goto leave;
  1998. }
  1999. /* get open lock so that only nodes can't remove it from orphan dir. */
  2000. status = ocfs2_open_lock(inode);
  2001. if (status < 0)
  2002. mlog_errno(status);
  2003. insert_inode_hash(inode);
  2004. leave:
  2005. if (status < 0 && did_quota_inode)
  2006. dquot_free_inode(inode);
  2007. if (handle)
  2008. ocfs2_commit_trans(osb, handle);
  2009. if (orphan_dir) {
  2010. /* This was locked for us in ocfs2_prepare_orphan_dir() */
  2011. ocfs2_inode_unlock(orphan_dir, 1);
  2012. mutex_unlock(&orphan_dir->i_mutex);
  2013. iput(orphan_dir);
  2014. }
  2015. if ((status < 0) && inode) {
  2016. clear_nlink(inode);
  2017. iput(inode);
  2018. }
  2019. if (inode_ac)
  2020. ocfs2_free_alloc_context(inode_ac);
  2021. brelse(new_di_bh);
  2022. if (!status)
  2023. *new_inode = inode;
  2024. ocfs2_free_dir_lookup_result(&orphan_insert);
  2025. ocfs2_inode_unlock(dir, 1);
  2026. brelse(parent_di_bh);
  2027. return status;
  2028. }
  2029. int ocfs2_mv_orphaned_inode_to_new(struct inode *dir,
  2030. struct inode *inode,
  2031. struct dentry *dentry)
  2032. {
  2033. int status = 0;
  2034. struct buffer_head *parent_di_bh = NULL;
  2035. handle_t *handle = NULL;
  2036. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  2037. struct ocfs2_dinode *dir_di, *di;
  2038. struct inode *orphan_dir_inode = NULL;
  2039. struct buffer_head *orphan_dir_bh = NULL;
  2040. struct buffer_head *di_bh = NULL;
  2041. struct ocfs2_dir_lookup_result lookup = { NULL, };
  2042. trace_ocfs2_mv_orphaned_inode_to_new(dir, dentry,
  2043. dentry->d_name.len, dentry->d_name.name,
  2044. (unsigned long long)OCFS2_I(dir)->ip_blkno,
  2045. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  2046. status = ocfs2_inode_lock(dir, &parent_di_bh, 1);
  2047. if (status < 0) {
  2048. if (status != -ENOENT)
  2049. mlog_errno(status);
  2050. return status;
  2051. }
  2052. dir_di = (struct ocfs2_dinode *) parent_di_bh->b_data;
  2053. if (!dir_di->i_links_count) {
  2054. /* can't make a file in a deleted directory. */
  2055. status = -ENOENT;
  2056. goto leave;
  2057. }
  2058. status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
  2059. dentry->d_name.len);
  2060. if (status)
  2061. goto leave;
  2062. /* get a spot inside the dir. */
  2063. status = ocfs2_prepare_dir_for_insert(osb, dir, parent_di_bh,
  2064. dentry->d_name.name,
  2065. dentry->d_name.len, &lookup);
  2066. if (status < 0) {
  2067. mlog_errno(status);
  2068. goto leave;
  2069. }
  2070. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  2071. ORPHAN_DIR_SYSTEM_INODE,
  2072. osb->slot_num);
  2073. if (!orphan_dir_inode) {
  2074. status = -EEXIST;
  2075. mlog_errno(status);
  2076. goto leave;
  2077. }
  2078. mutex_lock(&orphan_dir_inode->i_mutex);
  2079. status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
  2080. if (status < 0) {
  2081. mlog_errno(status);
  2082. mutex_unlock(&orphan_dir_inode->i_mutex);
  2083. iput(orphan_dir_inode);
  2084. goto leave;
  2085. }
  2086. status = ocfs2_read_inode_block(inode, &di_bh);
  2087. if (status < 0) {
  2088. mlog_errno(status);
  2089. goto orphan_unlock;
  2090. }
  2091. handle = ocfs2_start_trans(osb, ocfs2_rename_credits(osb->sb));
  2092. if (IS_ERR(handle)) {
  2093. status = PTR_ERR(handle);
  2094. handle = NULL;
  2095. mlog_errno(status);
  2096. goto orphan_unlock;
  2097. }
  2098. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
  2099. di_bh, OCFS2_JOURNAL_ACCESS_WRITE);
  2100. if (status < 0) {
  2101. mlog_errno(status);
  2102. goto out_commit;
  2103. }
  2104. status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode,
  2105. orphan_dir_bh);
  2106. if (status < 0) {
  2107. mlog_errno(status);
  2108. goto out_commit;
  2109. }
  2110. di = (struct ocfs2_dinode *)di_bh->b_data;
  2111. di->i_flags &= ~cpu_to_le32(OCFS2_ORPHANED_FL);
  2112. di->i_orphaned_slot = 0;
  2113. set_nlink(inode, 1);
  2114. ocfs2_set_links_count(di, inode->i_nlink);
  2115. ocfs2_journal_dirty(handle, di_bh);
  2116. status = ocfs2_add_entry(handle, dentry, inode,
  2117. OCFS2_I(inode)->ip_blkno, parent_di_bh,
  2118. &lookup);
  2119. if (status < 0) {
  2120. mlog_errno(status);
  2121. goto out_commit;
  2122. }
  2123. status = ocfs2_dentry_attach_lock(dentry, inode,
  2124. OCFS2_I(dir)->ip_blkno);
  2125. if (status) {
  2126. mlog_errno(status);
  2127. goto out_commit;
  2128. }
  2129. d_instantiate(dentry, inode);
  2130. status = 0;
  2131. out_commit:
  2132. ocfs2_commit_trans(osb, handle);
  2133. orphan_unlock:
  2134. ocfs2_inode_unlock(orphan_dir_inode, 1);
  2135. mutex_unlock(&orphan_dir_inode->i_mutex);
  2136. iput(orphan_dir_inode);
  2137. leave:
  2138. ocfs2_inode_unlock(dir, 1);
  2139. brelse(di_bh);
  2140. brelse(parent_di_bh);
  2141. brelse(orphan_dir_bh);
  2142. ocfs2_free_dir_lookup_result(&lookup);
  2143. if (status)
  2144. mlog_errno(status);
  2145. return status;
  2146. }
  2147. const struct inode_operations ocfs2_dir_iops = {
  2148. .create = ocfs2_create,
  2149. .lookup = ocfs2_lookup,
  2150. .link = ocfs2_link,
  2151. .unlink = ocfs2_unlink,
  2152. .rmdir = ocfs2_unlink,
  2153. .symlink = ocfs2_symlink,
  2154. .mkdir = ocfs2_mkdir,
  2155. .mknod = ocfs2_mknod,
  2156. .rename = ocfs2_rename,
  2157. .setattr = ocfs2_setattr,
  2158. .getattr = ocfs2_getattr,
  2159. .permission = ocfs2_permission,
  2160. .setxattr = generic_setxattr,
  2161. .getxattr = generic_getxattr,
  2162. .listxattr = ocfs2_listxattr,
  2163. .removexattr = generic_removexattr,
  2164. .fiemap = ocfs2_fiemap,
  2165. .get_acl = ocfs2_iop_get_acl,
  2166. .set_acl = ocfs2_iop_set_acl,
  2167. };