namei.c 70 KB

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