namei.c 70 KB

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