namei.c 70 KB

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