namei.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921
  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. struct buffer_head **tmpbh;
  953. struct inode *tmpinode;
  954. trace_ocfs2_double_lock((unsigned long long)oi1->ip_blkno,
  955. (unsigned long long)oi2->ip_blkno);
  956. if (*bh1)
  957. *bh1 = NULL;
  958. if (*bh2)
  959. *bh2 = NULL;
  960. /* we always want to lock the one with the lower lockid first.
  961. * and if they are nested, we lock ancestor first */
  962. if (oi1->ip_blkno != oi2->ip_blkno) {
  963. inode1_is_ancestor = ocfs2_check_if_ancestor(osb, oi2->ip_blkno,
  964. oi1->ip_blkno);
  965. if (inode1_is_ancestor < 0) {
  966. status = inode1_is_ancestor;
  967. goto bail;
  968. }
  969. inode2_is_ancestor = ocfs2_check_if_ancestor(osb, oi1->ip_blkno,
  970. oi2->ip_blkno);
  971. if (inode2_is_ancestor < 0) {
  972. status = inode2_is_ancestor;
  973. goto bail;
  974. }
  975. if ((inode1_is_ancestor == 1) ||
  976. (oi1->ip_blkno < oi2->ip_blkno &&
  977. inode2_is_ancestor == 0)) {
  978. /* switch id1 and id2 around */
  979. tmpbh = bh2;
  980. bh2 = bh1;
  981. bh1 = tmpbh;
  982. tmpinode = inode2;
  983. inode2 = inode1;
  984. inode1 = tmpinode;
  985. }
  986. /* lock id2 */
  987. status = ocfs2_inode_lock_nested(inode2, bh2, 1,
  988. rename == 1 ? OI_LS_RENAME1 : OI_LS_PARENT);
  989. if (status < 0) {
  990. if (status != -ENOENT)
  991. mlog_errno(status);
  992. goto bail;
  993. }
  994. }
  995. /* lock id1 */
  996. status = ocfs2_inode_lock_nested(inode1, bh1, 1,
  997. rename == 1 ? OI_LS_RENAME2 : OI_LS_PARENT);
  998. if (status < 0) {
  999. /*
  1000. * An error return must mean that no cluster locks
  1001. * were held on function exit.
  1002. */
  1003. if (oi1->ip_blkno != oi2->ip_blkno) {
  1004. ocfs2_inode_unlock(inode2, 1);
  1005. brelse(*bh2);
  1006. *bh2 = NULL;
  1007. }
  1008. if (status != -ENOENT)
  1009. mlog_errno(status);
  1010. }
  1011. trace_ocfs2_double_lock_end(
  1012. (unsigned long long)OCFS2_I(inode1)->ip_blkno,
  1013. (unsigned long long)OCFS2_I(inode2)->ip_blkno);
  1014. bail:
  1015. if (status)
  1016. mlog_errno(status);
  1017. return status;
  1018. }
  1019. static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2)
  1020. {
  1021. ocfs2_inode_unlock(inode1, 1);
  1022. if (inode1 != inode2)
  1023. ocfs2_inode_unlock(inode2, 1);
  1024. }
  1025. static int ocfs2_rename(struct inode *old_dir,
  1026. struct dentry *old_dentry,
  1027. struct inode *new_dir,
  1028. struct dentry *new_dentry)
  1029. {
  1030. int status = 0, rename_lock = 0, parents_locked = 0, target_exists = 0;
  1031. int old_child_locked = 0, new_child_locked = 0, update_dot_dot = 0;
  1032. struct inode *old_inode = d_inode(old_dentry);
  1033. struct inode *new_inode = d_inode(new_dentry);
  1034. struct inode *orphan_dir = NULL;
  1035. struct ocfs2_dinode *newfe = NULL;
  1036. char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
  1037. struct buffer_head *newfe_bh = NULL;
  1038. struct buffer_head *old_inode_bh = NULL;
  1039. struct ocfs2_super *osb = NULL;
  1040. u64 newfe_blkno, old_de_ino;
  1041. handle_t *handle = NULL;
  1042. struct buffer_head *old_dir_bh = NULL;
  1043. struct buffer_head *new_dir_bh = NULL;
  1044. u32 old_dir_nlink = old_dir->i_nlink;
  1045. struct ocfs2_dinode *old_di;
  1046. struct ocfs2_dir_lookup_result old_inode_dot_dot_res = { NULL, };
  1047. struct ocfs2_dir_lookup_result target_lookup_res = { NULL, };
  1048. struct ocfs2_dir_lookup_result old_entry_lookup = { NULL, };
  1049. struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
  1050. struct ocfs2_dir_lookup_result target_insert = { NULL, };
  1051. bool should_add_orphan = false;
  1052. /* At some point it might be nice to break this function up a
  1053. * bit. */
  1054. trace_ocfs2_rename(old_dir, old_dentry, new_dir, new_dentry,
  1055. old_dentry->d_name.len, old_dentry->d_name.name,
  1056. new_dentry->d_name.len, new_dentry->d_name.name);
  1057. dquot_initialize(old_dir);
  1058. dquot_initialize(new_dir);
  1059. osb = OCFS2_SB(old_dir->i_sb);
  1060. if (new_inode) {
  1061. if (!igrab(new_inode))
  1062. BUG();
  1063. }
  1064. /* Assume a directory hierarchy thusly:
  1065. * a/b/c
  1066. * a/d
  1067. * a,b,c, and d are all directories.
  1068. *
  1069. * from cwd of 'a' on both nodes:
  1070. * node1: mv b/c d
  1071. * node2: mv d b/c
  1072. *
  1073. * And that's why, just like the VFS, we need a file system
  1074. * rename lock. */
  1075. if (old_dir != new_dir && S_ISDIR(old_inode->i_mode)) {
  1076. status = ocfs2_rename_lock(osb);
  1077. if (status < 0) {
  1078. mlog_errno(status);
  1079. goto bail;
  1080. }
  1081. rename_lock = 1;
  1082. /* here we cannot guarantee the inodes haven't just been
  1083. * changed, so check if they are nested again */
  1084. status = ocfs2_check_if_ancestor(osb, new_dir->i_ino,
  1085. old_inode->i_ino);
  1086. if (status < 0) {
  1087. mlog_errno(status);
  1088. goto bail;
  1089. } else if (status == 1) {
  1090. status = -EPERM;
  1091. trace_ocfs2_rename_not_permitted(
  1092. (unsigned long long)old_inode->i_ino,
  1093. (unsigned long long)new_dir->i_ino);
  1094. goto bail;
  1095. }
  1096. }
  1097. /* if old and new are the same, this'll just do one lock. */
  1098. status = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
  1099. &new_dir_bh, new_dir, 1);
  1100. if (status < 0) {
  1101. mlog_errno(status);
  1102. goto bail;
  1103. }
  1104. parents_locked = 1;
  1105. /* make sure both dirs have bhs
  1106. * get an extra ref on old_dir_bh if old==new */
  1107. if (!new_dir_bh) {
  1108. if (old_dir_bh) {
  1109. new_dir_bh = old_dir_bh;
  1110. get_bh(new_dir_bh);
  1111. } else {
  1112. mlog(ML_ERROR, "no old_dir_bh!\n");
  1113. status = -EIO;
  1114. goto bail;
  1115. }
  1116. }
  1117. /*
  1118. * Aside from allowing a meta data update, the locking here
  1119. * also ensures that the downconvert thread on other nodes
  1120. * won't have to concurrently downconvert the inode and the
  1121. * dentry locks.
  1122. */
  1123. status = ocfs2_inode_lock_nested(old_inode, &old_inode_bh, 1,
  1124. OI_LS_PARENT);
  1125. if (status < 0) {
  1126. if (status != -ENOENT)
  1127. mlog_errno(status);
  1128. goto bail;
  1129. }
  1130. old_child_locked = 1;
  1131. status = ocfs2_remote_dentry_delete(old_dentry);
  1132. if (status < 0) {
  1133. mlog_errno(status);
  1134. goto bail;
  1135. }
  1136. if (S_ISDIR(old_inode->i_mode)) {
  1137. u64 old_inode_parent;
  1138. update_dot_dot = 1;
  1139. status = ocfs2_find_files_on_disk("..", 2, &old_inode_parent,
  1140. old_inode,
  1141. &old_inode_dot_dot_res);
  1142. if (status) {
  1143. status = -EIO;
  1144. goto bail;
  1145. }
  1146. if (old_inode_parent != OCFS2_I(old_dir)->ip_blkno) {
  1147. status = -EIO;
  1148. goto bail;
  1149. }
  1150. if (!new_inode && new_dir != old_dir &&
  1151. new_dir->i_nlink >= ocfs2_link_max(osb)) {
  1152. status = -EMLINK;
  1153. goto bail;
  1154. }
  1155. }
  1156. status = ocfs2_lookup_ino_from_name(old_dir, old_dentry->d_name.name,
  1157. old_dentry->d_name.len,
  1158. &old_de_ino);
  1159. if (status) {
  1160. status = -ENOENT;
  1161. goto bail;
  1162. }
  1163. /*
  1164. * Check for inode number is _not_ due to possible IO errors.
  1165. * We might rmdir the source, keep it as pwd of some process
  1166. * and merrily kill the link to whatever was created under the
  1167. * same name. Goodbye sticky bit ;-<
  1168. */
  1169. if (old_de_ino != OCFS2_I(old_inode)->ip_blkno) {
  1170. status = -ENOENT;
  1171. goto bail;
  1172. }
  1173. /* check if the target already exists (in which case we need
  1174. * to delete it */
  1175. status = ocfs2_find_files_on_disk(new_dentry->d_name.name,
  1176. new_dentry->d_name.len,
  1177. &newfe_blkno, new_dir,
  1178. &target_lookup_res);
  1179. /* The only error we allow here is -ENOENT because the new
  1180. * file not existing is perfectly valid. */
  1181. if ((status < 0) && (status != -ENOENT)) {
  1182. /* If we cannot find the file specified we should just */
  1183. /* return the error... */
  1184. mlog_errno(status);
  1185. goto bail;
  1186. }
  1187. if (status == 0)
  1188. target_exists = 1;
  1189. if (!target_exists && new_inode) {
  1190. /*
  1191. * Target was unlinked by another node while we were
  1192. * waiting to get to ocfs2_rename(). There isn't
  1193. * anything we can do here to help the situation, so
  1194. * bubble up the appropriate error.
  1195. */
  1196. status = -ENOENT;
  1197. goto bail;
  1198. }
  1199. /* In case we need to overwrite an existing file, we blow it
  1200. * away first */
  1201. if (target_exists) {
  1202. /* VFS didn't think there existed an inode here, but
  1203. * someone else in the cluster must have raced our
  1204. * rename to create one. Today we error cleanly, in
  1205. * the future we should consider calling iget to build
  1206. * a new struct inode for this entry. */
  1207. if (!new_inode) {
  1208. status = -EACCES;
  1209. trace_ocfs2_rename_target_exists(new_dentry->d_name.len,
  1210. new_dentry->d_name.name);
  1211. goto bail;
  1212. }
  1213. if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
  1214. status = -EACCES;
  1215. trace_ocfs2_rename_disagree(
  1216. (unsigned long long)OCFS2_I(new_inode)->ip_blkno,
  1217. (unsigned long long)newfe_blkno,
  1218. OCFS2_I(new_inode)->ip_flags);
  1219. goto bail;
  1220. }
  1221. status = ocfs2_inode_lock(new_inode, &newfe_bh, 1);
  1222. if (status < 0) {
  1223. if (status != -ENOENT)
  1224. mlog_errno(status);
  1225. goto bail;
  1226. }
  1227. new_child_locked = 1;
  1228. status = ocfs2_remote_dentry_delete(new_dentry);
  1229. if (status < 0) {
  1230. mlog_errno(status);
  1231. goto bail;
  1232. }
  1233. newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
  1234. trace_ocfs2_rename_over_existing(
  1235. (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
  1236. (unsigned long long)newfe_bh->b_blocknr : 0ULL);
  1237. if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
  1238. status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
  1239. OCFS2_I(new_inode)->ip_blkno,
  1240. orphan_name, &orphan_insert,
  1241. false);
  1242. if (status < 0) {
  1243. mlog_errno(status);
  1244. goto bail;
  1245. }
  1246. should_add_orphan = true;
  1247. }
  1248. } else {
  1249. BUG_ON(d_inode(new_dentry->d_parent) != new_dir);
  1250. status = ocfs2_check_dir_for_entry(new_dir,
  1251. new_dentry->d_name.name,
  1252. new_dentry->d_name.len);
  1253. if (status)
  1254. goto bail;
  1255. status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,
  1256. new_dentry->d_name.name,
  1257. new_dentry->d_name.len,
  1258. &target_insert);
  1259. if (status < 0) {
  1260. mlog_errno(status);
  1261. goto bail;
  1262. }
  1263. }
  1264. handle = ocfs2_start_trans(osb, ocfs2_rename_credits(osb->sb));
  1265. if (IS_ERR(handle)) {
  1266. status = PTR_ERR(handle);
  1267. handle = NULL;
  1268. mlog_errno(status);
  1269. goto bail;
  1270. }
  1271. if (target_exists) {
  1272. if (S_ISDIR(new_inode->i_mode)) {
  1273. if (new_inode->i_nlink != 2 ||
  1274. !ocfs2_empty_dir(new_inode)) {
  1275. status = -ENOTEMPTY;
  1276. goto bail;
  1277. }
  1278. }
  1279. status = ocfs2_journal_access_di(handle, INODE_CACHE(new_inode),
  1280. newfe_bh,
  1281. OCFS2_JOURNAL_ACCESS_WRITE);
  1282. if (status < 0) {
  1283. mlog_errno(status);
  1284. goto bail;
  1285. }
  1286. /* change the dirent to point to the correct inode */
  1287. status = ocfs2_update_entry(new_dir, handle, &target_lookup_res,
  1288. old_inode);
  1289. if (status < 0) {
  1290. mlog_errno(status);
  1291. goto bail;
  1292. }
  1293. new_dir->i_version++;
  1294. if (S_ISDIR(new_inode->i_mode))
  1295. ocfs2_set_links_count(newfe, 0);
  1296. else
  1297. ocfs2_add_links_count(newfe, -1);
  1298. ocfs2_journal_dirty(handle, newfe_bh);
  1299. if (should_add_orphan) {
  1300. status = ocfs2_orphan_add(osb, handle, new_inode,
  1301. newfe_bh, orphan_name,
  1302. &orphan_insert, orphan_dir, false);
  1303. if (status < 0) {
  1304. mlog_errno(status);
  1305. goto bail;
  1306. }
  1307. }
  1308. } else {
  1309. /* if the name was not found in new_dir, add it now */
  1310. status = ocfs2_add_entry(handle, new_dentry, old_inode,
  1311. OCFS2_I(old_inode)->ip_blkno,
  1312. new_dir_bh, &target_insert);
  1313. }
  1314. old_inode->i_ctime = CURRENT_TIME;
  1315. mark_inode_dirty(old_inode);
  1316. status = ocfs2_journal_access_di(handle, INODE_CACHE(old_inode),
  1317. old_inode_bh,
  1318. OCFS2_JOURNAL_ACCESS_WRITE);
  1319. if (status >= 0) {
  1320. old_di = (struct ocfs2_dinode *) old_inode_bh->b_data;
  1321. old_di->i_ctime = cpu_to_le64(old_inode->i_ctime.tv_sec);
  1322. old_di->i_ctime_nsec = cpu_to_le32(old_inode->i_ctime.tv_nsec);
  1323. ocfs2_journal_dirty(handle, old_inode_bh);
  1324. } else
  1325. mlog_errno(status);
  1326. /*
  1327. * Now that the name has been added to new_dir, remove the old name.
  1328. *
  1329. * We don't keep any directory entry context around until now
  1330. * because the insert might have changed the type of directory
  1331. * we're dealing with.
  1332. */
  1333. status = ocfs2_find_entry(old_dentry->d_name.name,
  1334. old_dentry->d_name.len, old_dir,
  1335. &old_entry_lookup);
  1336. if (status)
  1337. goto bail;
  1338. status = ocfs2_delete_entry(handle, old_dir, &old_entry_lookup);
  1339. if (status < 0) {
  1340. mlog_errno(status);
  1341. goto bail;
  1342. }
  1343. if (new_inode) {
  1344. drop_nlink(new_inode);
  1345. new_inode->i_ctime = CURRENT_TIME;
  1346. }
  1347. old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
  1348. if (update_dot_dot) {
  1349. status = ocfs2_update_entry(old_inode, handle,
  1350. &old_inode_dot_dot_res, new_dir);
  1351. drop_nlink(old_dir);
  1352. if (new_inode) {
  1353. drop_nlink(new_inode);
  1354. } else {
  1355. inc_nlink(new_dir);
  1356. mark_inode_dirty(new_dir);
  1357. }
  1358. }
  1359. mark_inode_dirty(old_dir);
  1360. ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh);
  1361. if (new_inode) {
  1362. mark_inode_dirty(new_inode);
  1363. ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh);
  1364. }
  1365. if (old_dir != new_dir) {
  1366. /* Keep the same times on both directories.*/
  1367. new_dir->i_ctime = new_dir->i_mtime = old_dir->i_ctime;
  1368. /*
  1369. * This will also pick up the i_nlink change from the
  1370. * block above.
  1371. */
  1372. ocfs2_mark_inode_dirty(handle, new_dir, new_dir_bh);
  1373. }
  1374. if (old_dir_nlink != old_dir->i_nlink) {
  1375. if (!old_dir_bh) {
  1376. mlog(ML_ERROR, "need to change nlink for old dir "
  1377. "%llu from %d to %d but bh is NULL!\n",
  1378. (unsigned long long)OCFS2_I(old_dir)->ip_blkno,
  1379. (int)old_dir_nlink, old_dir->i_nlink);
  1380. } else {
  1381. struct ocfs2_dinode *fe;
  1382. status = ocfs2_journal_access_di(handle,
  1383. INODE_CACHE(old_dir),
  1384. old_dir_bh,
  1385. OCFS2_JOURNAL_ACCESS_WRITE);
  1386. fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
  1387. ocfs2_set_links_count(fe, old_dir->i_nlink);
  1388. ocfs2_journal_dirty(handle, old_dir_bh);
  1389. }
  1390. }
  1391. ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
  1392. status = 0;
  1393. bail:
  1394. if (rename_lock)
  1395. ocfs2_rename_unlock(osb);
  1396. if (handle)
  1397. ocfs2_commit_trans(osb, handle);
  1398. if (parents_locked)
  1399. ocfs2_double_unlock(old_dir, new_dir);
  1400. if (old_child_locked)
  1401. ocfs2_inode_unlock(old_inode, 1);
  1402. if (new_child_locked)
  1403. ocfs2_inode_unlock(new_inode, 1);
  1404. if (orphan_dir) {
  1405. /* This was locked for us in ocfs2_prepare_orphan_dir() */
  1406. ocfs2_inode_unlock(orphan_dir, 1);
  1407. mutex_unlock(&orphan_dir->i_mutex);
  1408. iput(orphan_dir);
  1409. }
  1410. if (new_inode)
  1411. sync_mapping_buffers(old_inode->i_mapping);
  1412. if (new_inode)
  1413. iput(new_inode);
  1414. ocfs2_free_dir_lookup_result(&target_lookup_res);
  1415. ocfs2_free_dir_lookup_result(&old_entry_lookup);
  1416. ocfs2_free_dir_lookup_result(&old_inode_dot_dot_res);
  1417. ocfs2_free_dir_lookup_result(&orphan_insert);
  1418. ocfs2_free_dir_lookup_result(&target_insert);
  1419. brelse(newfe_bh);
  1420. brelse(old_inode_bh);
  1421. brelse(old_dir_bh);
  1422. brelse(new_dir_bh);
  1423. if (status)
  1424. mlog_errno(status);
  1425. return status;
  1426. }
  1427. /*
  1428. * we expect i_size = strlen(symname). Copy symname into the file
  1429. * data, including the null terminator.
  1430. */
  1431. static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
  1432. handle_t *handle,
  1433. struct inode *inode,
  1434. const char *symname)
  1435. {
  1436. struct buffer_head **bhs = NULL;
  1437. const char *c;
  1438. struct super_block *sb = osb->sb;
  1439. u64 p_blkno, p_blocks;
  1440. int virtual, blocks, status, i, bytes_left;
  1441. bytes_left = i_size_read(inode) + 1;
  1442. /* we can't trust i_blocks because we're actually going to
  1443. * write i_size + 1 bytes. */
  1444. blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
  1445. trace_ocfs2_create_symlink_data((unsigned long long)inode->i_blocks,
  1446. i_size_read(inode), blocks);
  1447. /* Sanity check -- make sure we're going to fit. */
  1448. if (bytes_left >
  1449. ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
  1450. status = -EIO;
  1451. mlog_errno(status);
  1452. goto bail;
  1453. }
  1454. bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
  1455. if (!bhs) {
  1456. status = -ENOMEM;
  1457. mlog_errno(status);
  1458. goto bail;
  1459. }
  1460. status = ocfs2_extent_map_get_blocks(inode, 0, &p_blkno, &p_blocks,
  1461. NULL);
  1462. if (status < 0) {
  1463. mlog_errno(status);
  1464. goto bail;
  1465. }
  1466. /* links can never be larger than one cluster so we know this
  1467. * is all going to be contiguous, but do a sanity check
  1468. * anyway. */
  1469. if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
  1470. status = -EIO;
  1471. mlog_errno(status);
  1472. goto bail;
  1473. }
  1474. virtual = 0;
  1475. while(bytes_left > 0) {
  1476. c = &symname[virtual * sb->s_blocksize];
  1477. bhs[virtual] = sb_getblk(sb, p_blkno);
  1478. if (!bhs[virtual]) {
  1479. status = -ENOMEM;
  1480. mlog_errno(status);
  1481. goto bail;
  1482. }
  1483. ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode),
  1484. bhs[virtual]);
  1485. status = ocfs2_journal_access(handle, INODE_CACHE(inode),
  1486. bhs[virtual],
  1487. OCFS2_JOURNAL_ACCESS_CREATE);
  1488. if (status < 0) {
  1489. mlog_errno(status);
  1490. goto bail;
  1491. }
  1492. memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
  1493. memcpy(bhs[virtual]->b_data, c,
  1494. (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
  1495. bytes_left);
  1496. ocfs2_journal_dirty(handle, bhs[virtual]);
  1497. virtual++;
  1498. p_blkno++;
  1499. bytes_left -= sb->s_blocksize;
  1500. }
  1501. status = 0;
  1502. bail:
  1503. if (bhs) {
  1504. for(i = 0; i < blocks; i++)
  1505. brelse(bhs[i]);
  1506. kfree(bhs);
  1507. }
  1508. if (status)
  1509. mlog_errno(status);
  1510. return status;
  1511. }
  1512. static int ocfs2_symlink(struct inode *dir,
  1513. struct dentry *dentry,
  1514. const char *symname)
  1515. {
  1516. int status, l, credits;
  1517. u64 newsize;
  1518. struct ocfs2_super *osb = NULL;
  1519. struct inode *inode = NULL;
  1520. struct super_block *sb;
  1521. struct buffer_head *new_fe_bh = NULL;
  1522. struct buffer_head *parent_fe_bh = NULL;
  1523. struct ocfs2_dinode *fe = NULL;
  1524. struct ocfs2_dinode *dirfe;
  1525. handle_t *handle = NULL;
  1526. struct ocfs2_alloc_context *inode_ac = NULL;
  1527. struct ocfs2_alloc_context *data_ac = NULL;
  1528. struct ocfs2_alloc_context *xattr_ac = NULL;
  1529. int want_clusters = 0;
  1530. int xattr_credits = 0;
  1531. struct ocfs2_security_xattr_info si = {
  1532. .enable = 1,
  1533. };
  1534. int did_quota = 0, did_quota_inode = 0;
  1535. struct ocfs2_dir_lookup_result lookup = { NULL, };
  1536. sigset_t oldset;
  1537. int did_block_signals = 0;
  1538. struct ocfs2_dentry_lock *dl = NULL;
  1539. trace_ocfs2_symlink_begin(dir, dentry, symname,
  1540. dentry->d_name.len, dentry->d_name.name);
  1541. dquot_initialize(dir);
  1542. sb = dir->i_sb;
  1543. osb = OCFS2_SB(sb);
  1544. l = strlen(symname) + 1;
  1545. credits = ocfs2_calc_symlink_credits(sb);
  1546. /* lock the parent directory */
  1547. status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
  1548. if (status < 0) {
  1549. if (status != -ENOENT)
  1550. mlog_errno(status);
  1551. return status;
  1552. }
  1553. dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
  1554. if (!ocfs2_read_links_count(dirfe)) {
  1555. /* can't make a file in a deleted directory. */
  1556. status = -ENOENT;
  1557. goto bail;
  1558. }
  1559. status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
  1560. dentry->d_name.len);
  1561. if (status)
  1562. goto bail;
  1563. status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
  1564. dentry->d_name.name,
  1565. dentry->d_name.len, &lookup);
  1566. if (status < 0) {
  1567. mlog_errno(status);
  1568. goto bail;
  1569. }
  1570. status = ocfs2_reserve_new_inode(osb, &inode_ac);
  1571. if (status < 0) {
  1572. if (status != -ENOSPC)
  1573. mlog_errno(status);
  1574. goto bail;
  1575. }
  1576. inode = ocfs2_get_init_inode(dir, S_IFLNK | S_IRWXUGO);
  1577. if (!inode) {
  1578. status = -ENOMEM;
  1579. mlog_errno(status);
  1580. goto bail;
  1581. }
  1582. /* get security xattr */
  1583. status = ocfs2_init_security_get(inode, dir, &dentry->d_name, &si);
  1584. if (status) {
  1585. if (status == -EOPNOTSUPP)
  1586. si.enable = 0;
  1587. else {
  1588. mlog_errno(status);
  1589. goto bail;
  1590. }
  1591. }
  1592. /* calculate meta data/clusters for setting security xattr */
  1593. if (si.enable) {
  1594. status = ocfs2_calc_security_init(dir, &si, &want_clusters,
  1595. &xattr_credits, &xattr_ac);
  1596. if (status < 0) {
  1597. mlog_errno(status);
  1598. goto bail;
  1599. }
  1600. }
  1601. /* don't reserve bitmap space for fast symlinks. */
  1602. if (l > ocfs2_fast_symlink_chars(sb))
  1603. want_clusters += 1;
  1604. status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac);
  1605. if (status < 0) {
  1606. if (status != -ENOSPC)
  1607. mlog_errno(status);
  1608. goto bail;
  1609. }
  1610. handle = ocfs2_start_trans(osb, credits + xattr_credits);
  1611. if (IS_ERR(handle)) {
  1612. status = PTR_ERR(handle);
  1613. handle = NULL;
  1614. mlog_errno(status);
  1615. goto bail;
  1616. }
  1617. /* Starting to change things, restart is no longer possible. */
  1618. ocfs2_block_signals(&oldset);
  1619. did_block_signals = 1;
  1620. status = dquot_alloc_inode(inode);
  1621. if (status)
  1622. goto bail;
  1623. did_quota_inode = 1;
  1624. trace_ocfs2_symlink_create(dir, dentry, dentry->d_name.len,
  1625. dentry->d_name.name,
  1626. (unsigned long long)OCFS2_I(dir)->ip_blkno,
  1627. inode->i_mode);
  1628. status = ocfs2_mknod_locked(osb, dir, inode,
  1629. 0, &new_fe_bh, parent_fe_bh, handle,
  1630. inode_ac);
  1631. if (status < 0) {
  1632. mlog_errno(status);
  1633. goto bail;
  1634. }
  1635. fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
  1636. inode->i_rdev = 0;
  1637. newsize = l - 1;
  1638. inode->i_op = &ocfs2_symlink_inode_operations;
  1639. if (l > ocfs2_fast_symlink_chars(sb)) {
  1640. u32 offset = 0;
  1641. status = dquot_alloc_space_nodirty(inode,
  1642. ocfs2_clusters_to_bytes(osb->sb, 1));
  1643. if (status)
  1644. goto bail;
  1645. did_quota = 1;
  1646. inode->i_mapping->a_ops = &ocfs2_aops;
  1647. status = ocfs2_add_inode_data(osb, inode, &offset, 1, 0,
  1648. new_fe_bh,
  1649. handle, data_ac, NULL,
  1650. NULL);
  1651. if (status < 0) {
  1652. if (status != -ENOSPC && status != -EINTR) {
  1653. mlog(ML_ERROR,
  1654. "Failed to extend file to %llu\n",
  1655. (unsigned long long)newsize);
  1656. mlog_errno(status);
  1657. status = -ENOSPC;
  1658. }
  1659. goto bail;
  1660. }
  1661. i_size_write(inode, newsize);
  1662. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1663. } else {
  1664. inode->i_mapping->a_ops = &ocfs2_fast_symlink_aops;
  1665. memcpy((char *) fe->id2.i_symlink, symname, l);
  1666. i_size_write(inode, newsize);
  1667. inode->i_blocks = 0;
  1668. }
  1669. status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
  1670. if (status < 0) {
  1671. mlog_errno(status);
  1672. goto bail;
  1673. }
  1674. if (!ocfs2_inode_is_fast_symlink(inode)) {
  1675. status = ocfs2_create_symlink_data(osb, handle, inode,
  1676. symname);
  1677. if (status < 0) {
  1678. mlog_errno(status);
  1679. goto bail;
  1680. }
  1681. }
  1682. if (si.enable) {
  1683. status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si,
  1684. xattr_ac, data_ac);
  1685. if (status < 0) {
  1686. mlog_errno(status);
  1687. goto bail;
  1688. }
  1689. }
  1690. /*
  1691. * Do this before adding the entry to the directory. We add
  1692. * also set d_op after success so that ->d_iput() will cleanup
  1693. * the dentry lock even if ocfs2_add_entry() fails below.
  1694. */
  1695. status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
  1696. if (status) {
  1697. mlog_errno(status);
  1698. goto bail;
  1699. }
  1700. dl = dentry->d_fsdata;
  1701. status = ocfs2_add_entry(handle, dentry, inode,
  1702. le64_to_cpu(fe->i_blkno), parent_fe_bh,
  1703. &lookup);
  1704. if (status < 0) {
  1705. mlog_errno(status);
  1706. goto bail;
  1707. }
  1708. insert_inode_hash(inode);
  1709. d_instantiate(dentry, inode);
  1710. bail:
  1711. if (status < 0 && did_quota)
  1712. dquot_free_space_nodirty(inode,
  1713. ocfs2_clusters_to_bytes(osb->sb, 1));
  1714. if (status < 0 && did_quota_inode)
  1715. dquot_free_inode(inode);
  1716. if (handle)
  1717. ocfs2_commit_trans(osb, handle);
  1718. ocfs2_inode_unlock(dir, 1);
  1719. if (did_block_signals)
  1720. ocfs2_unblock_signals(&oldset);
  1721. brelse(new_fe_bh);
  1722. brelse(parent_fe_bh);
  1723. kfree(si.value);
  1724. ocfs2_free_dir_lookup_result(&lookup);
  1725. if (inode_ac)
  1726. ocfs2_free_alloc_context(inode_ac);
  1727. if (data_ac)
  1728. ocfs2_free_alloc_context(data_ac);
  1729. if (xattr_ac)
  1730. ocfs2_free_alloc_context(xattr_ac);
  1731. if ((status < 0) && inode) {
  1732. if (dl)
  1733. ocfs2_cleanup_add_entry_failure(osb, dentry, inode);
  1734. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SKIP_ORPHAN_DIR;
  1735. clear_nlink(inode);
  1736. iput(inode);
  1737. }
  1738. if (status)
  1739. mlog_errno(status);
  1740. return status;
  1741. }
  1742. static int ocfs2_blkno_stringify(u64 blkno, char *name)
  1743. {
  1744. int status, namelen;
  1745. namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
  1746. (long long)blkno);
  1747. if (namelen <= 0) {
  1748. if (namelen)
  1749. status = namelen;
  1750. else
  1751. status = -EINVAL;
  1752. mlog_errno(status);
  1753. goto bail;
  1754. }
  1755. if (namelen != OCFS2_ORPHAN_NAMELEN) {
  1756. status = -EINVAL;
  1757. mlog_errno(status);
  1758. goto bail;
  1759. }
  1760. trace_ocfs2_blkno_stringify(blkno, name, namelen);
  1761. status = 0;
  1762. bail:
  1763. if (status < 0)
  1764. mlog_errno(status);
  1765. return status;
  1766. }
  1767. static int ocfs2_lookup_lock_orphan_dir(struct ocfs2_super *osb,
  1768. struct inode **ret_orphan_dir,
  1769. struct buffer_head **ret_orphan_dir_bh)
  1770. {
  1771. struct inode *orphan_dir_inode;
  1772. struct buffer_head *orphan_dir_bh = NULL;
  1773. int ret = 0;
  1774. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  1775. ORPHAN_DIR_SYSTEM_INODE,
  1776. osb->slot_num);
  1777. if (!orphan_dir_inode) {
  1778. ret = -ENOENT;
  1779. mlog_errno(ret);
  1780. return ret;
  1781. }
  1782. mutex_lock(&orphan_dir_inode->i_mutex);
  1783. ret = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
  1784. if (ret < 0) {
  1785. mutex_unlock(&orphan_dir_inode->i_mutex);
  1786. iput(orphan_dir_inode);
  1787. mlog_errno(ret);
  1788. return ret;
  1789. }
  1790. *ret_orphan_dir = orphan_dir_inode;
  1791. *ret_orphan_dir_bh = orphan_dir_bh;
  1792. return 0;
  1793. }
  1794. static int __ocfs2_prepare_orphan_dir(struct inode *orphan_dir_inode,
  1795. struct buffer_head *orphan_dir_bh,
  1796. u64 blkno,
  1797. char *name,
  1798. struct ocfs2_dir_lookup_result *lookup,
  1799. bool dio)
  1800. {
  1801. int ret;
  1802. struct ocfs2_super *osb = OCFS2_SB(orphan_dir_inode->i_sb);
  1803. int namelen = dio ?
  1804. (OCFS2_DIO_ORPHAN_PREFIX_LEN + OCFS2_ORPHAN_NAMELEN) :
  1805. OCFS2_ORPHAN_NAMELEN;
  1806. if (dio) {
  1807. ret = snprintf(name, OCFS2_DIO_ORPHAN_PREFIX_LEN + 1, "%s",
  1808. OCFS2_DIO_ORPHAN_PREFIX);
  1809. if (ret != OCFS2_DIO_ORPHAN_PREFIX_LEN) {
  1810. ret = -EINVAL;
  1811. mlog_errno(ret);
  1812. return ret;
  1813. }
  1814. ret = ocfs2_blkno_stringify(blkno,
  1815. name + OCFS2_DIO_ORPHAN_PREFIX_LEN);
  1816. } else
  1817. ret = ocfs2_blkno_stringify(blkno, name);
  1818. if (ret < 0) {
  1819. mlog_errno(ret);
  1820. return ret;
  1821. }
  1822. ret = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
  1823. orphan_dir_bh, name,
  1824. namelen, lookup);
  1825. if (ret < 0) {
  1826. mlog_errno(ret);
  1827. return ret;
  1828. }
  1829. return 0;
  1830. }
  1831. /**
  1832. * ocfs2_prepare_orphan_dir() - Prepare an orphan directory for
  1833. * insertion of an orphan.
  1834. * @osb: ocfs2 file system
  1835. * @ret_orphan_dir: Orphan dir inode - returned locked!
  1836. * @blkno: Actual block number of the inode to be inserted into orphan dir.
  1837. * @lookup: dir lookup result, to be passed back into functions like
  1838. * ocfs2_orphan_add
  1839. *
  1840. * Returns zero on success and the ret_orphan_dir, name and lookup
  1841. * fields will be populated.
  1842. *
  1843. * Returns non-zero on failure.
  1844. */
  1845. static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
  1846. struct inode **ret_orphan_dir,
  1847. u64 blkno,
  1848. char *name,
  1849. struct ocfs2_dir_lookup_result *lookup,
  1850. bool dio)
  1851. {
  1852. struct inode *orphan_dir_inode = NULL;
  1853. struct buffer_head *orphan_dir_bh = NULL;
  1854. int ret = 0;
  1855. ret = ocfs2_lookup_lock_orphan_dir(osb, &orphan_dir_inode,
  1856. &orphan_dir_bh);
  1857. if (ret < 0) {
  1858. mlog_errno(ret);
  1859. return ret;
  1860. }
  1861. ret = __ocfs2_prepare_orphan_dir(orphan_dir_inode, orphan_dir_bh,
  1862. blkno, name, lookup, dio);
  1863. if (ret < 0) {
  1864. mlog_errno(ret);
  1865. goto out;
  1866. }
  1867. *ret_orphan_dir = orphan_dir_inode;
  1868. out:
  1869. brelse(orphan_dir_bh);
  1870. if (ret) {
  1871. ocfs2_inode_unlock(orphan_dir_inode, 1);
  1872. mutex_unlock(&orphan_dir_inode->i_mutex);
  1873. iput(orphan_dir_inode);
  1874. }
  1875. if (ret)
  1876. mlog_errno(ret);
  1877. return ret;
  1878. }
  1879. static int ocfs2_orphan_add(struct ocfs2_super *osb,
  1880. handle_t *handle,
  1881. struct inode *inode,
  1882. struct buffer_head *fe_bh,
  1883. char *name,
  1884. struct ocfs2_dir_lookup_result *lookup,
  1885. struct inode *orphan_dir_inode,
  1886. bool dio)
  1887. {
  1888. struct buffer_head *orphan_dir_bh = NULL;
  1889. int status = 0;
  1890. struct ocfs2_dinode *orphan_fe;
  1891. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
  1892. int namelen = dio ?
  1893. (OCFS2_DIO_ORPHAN_PREFIX_LEN + OCFS2_ORPHAN_NAMELEN) :
  1894. OCFS2_ORPHAN_NAMELEN;
  1895. trace_ocfs2_orphan_add_begin(
  1896. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1897. status = ocfs2_read_inode_block(orphan_dir_inode, &orphan_dir_bh);
  1898. if (status < 0) {
  1899. mlog_errno(status);
  1900. goto leave;
  1901. }
  1902. status = ocfs2_journal_access_di(handle,
  1903. INODE_CACHE(orphan_dir_inode),
  1904. orphan_dir_bh,
  1905. OCFS2_JOURNAL_ACCESS_WRITE);
  1906. if (status < 0) {
  1907. mlog_errno(status);
  1908. goto leave;
  1909. }
  1910. /*
  1911. * We're going to journal the change of i_flags and i_orphaned_slot.
  1912. * It's safe anyway, though some callers may duplicate the journaling.
  1913. * Journaling within the func just make the logic look more
  1914. * straightforward.
  1915. */
  1916. status = ocfs2_journal_access_di(handle,
  1917. INODE_CACHE(inode),
  1918. fe_bh,
  1919. OCFS2_JOURNAL_ACCESS_WRITE);
  1920. if (status < 0) {
  1921. mlog_errno(status);
  1922. goto leave;
  1923. }
  1924. /* we're a cluster, and nlink can change on disk from
  1925. * underneath us... */
  1926. orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
  1927. if (S_ISDIR(inode->i_mode))
  1928. ocfs2_add_links_count(orphan_fe, 1);
  1929. set_nlink(orphan_dir_inode, ocfs2_read_links_count(orphan_fe));
  1930. ocfs2_journal_dirty(handle, orphan_dir_bh);
  1931. status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
  1932. namelen, inode,
  1933. OCFS2_I(inode)->ip_blkno,
  1934. orphan_dir_bh, lookup);
  1935. if (status < 0) {
  1936. mlog_errno(status);
  1937. goto rollback;
  1938. }
  1939. if (dio) {
  1940. /* Update flag OCFS2_DIO_ORPHANED_FL and record the orphan
  1941. * slot.
  1942. */
  1943. fe->i_flags |= cpu_to_le32(OCFS2_DIO_ORPHANED_FL);
  1944. fe->i_dio_orphaned_slot = cpu_to_le16(osb->slot_num);
  1945. } else {
  1946. fe->i_flags |= cpu_to_le32(OCFS2_ORPHANED_FL);
  1947. OCFS2_I(inode)->ip_flags &= ~OCFS2_INODE_SKIP_ORPHAN_DIR;
  1948. /* Record which orphan dir our inode now resides
  1949. * in. delete_inode will use this to determine which orphan
  1950. * dir to lock. */
  1951. fe->i_orphaned_slot = cpu_to_le16(osb->slot_num);
  1952. }
  1953. ocfs2_journal_dirty(handle, fe_bh);
  1954. trace_ocfs2_orphan_add_end((unsigned long long)OCFS2_I(inode)->ip_blkno,
  1955. osb->slot_num);
  1956. rollback:
  1957. if (status < 0) {
  1958. if (S_ISDIR(inode->i_mode))
  1959. ocfs2_add_links_count(orphan_fe, -1);
  1960. set_nlink(orphan_dir_inode, ocfs2_read_links_count(orphan_fe));
  1961. }
  1962. leave:
  1963. brelse(orphan_dir_bh);
  1964. return status;
  1965. }
  1966. /* unlike orphan_add, we expect the orphan dir to already be locked here. */
  1967. int ocfs2_orphan_del(struct ocfs2_super *osb,
  1968. handle_t *handle,
  1969. struct inode *orphan_dir_inode,
  1970. struct inode *inode,
  1971. struct buffer_head *orphan_dir_bh,
  1972. bool dio)
  1973. {
  1974. const int namelen = OCFS2_DIO_ORPHAN_PREFIX_LEN + OCFS2_ORPHAN_NAMELEN;
  1975. char name[namelen + 1];
  1976. struct ocfs2_dinode *orphan_fe;
  1977. int status = 0;
  1978. struct ocfs2_dir_lookup_result lookup = { NULL, };
  1979. if (dio) {
  1980. status = snprintf(name, OCFS2_DIO_ORPHAN_PREFIX_LEN + 1, "%s",
  1981. OCFS2_DIO_ORPHAN_PREFIX);
  1982. if (status != OCFS2_DIO_ORPHAN_PREFIX_LEN) {
  1983. status = -EINVAL;
  1984. mlog_errno(status);
  1985. return status;
  1986. }
  1987. status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno,
  1988. name + OCFS2_DIO_ORPHAN_PREFIX_LEN);
  1989. } else
  1990. status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
  1991. if (status < 0) {
  1992. mlog_errno(status);
  1993. goto leave;
  1994. }
  1995. trace_ocfs2_orphan_del(
  1996. (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
  1997. name, strlen(name));
  1998. /* find it's spot in the orphan directory */
  1999. status = ocfs2_find_entry(name, strlen(name), orphan_dir_inode,
  2000. &lookup);
  2001. if (status) {
  2002. mlog_errno(status);
  2003. goto leave;
  2004. }
  2005. /* remove it from the orphan directory */
  2006. status = ocfs2_delete_entry(handle, orphan_dir_inode, &lookup);
  2007. if (status < 0) {
  2008. mlog_errno(status);
  2009. goto leave;
  2010. }
  2011. status = ocfs2_journal_access_di(handle,
  2012. INODE_CACHE(orphan_dir_inode),
  2013. orphan_dir_bh,
  2014. OCFS2_JOURNAL_ACCESS_WRITE);
  2015. if (status < 0) {
  2016. mlog_errno(status);
  2017. goto leave;
  2018. }
  2019. /* do the i_nlink dance! :) */
  2020. orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
  2021. if (S_ISDIR(inode->i_mode))
  2022. ocfs2_add_links_count(orphan_fe, -1);
  2023. set_nlink(orphan_dir_inode, ocfs2_read_links_count(orphan_fe));
  2024. ocfs2_journal_dirty(handle, orphan_dir_bh);
  2025. leave:
  2026. ocfs2_free_dir_lookup_result(&lookup);
  2027. if (status)
  2028. mlog_errno(status);
  2029. return status;
  2030. }
  2031. /**
  2032. * ocfs2_prep_new_orphaned_file() - Prepare the orphan dir to receive a newly
  2033. * allocated file. This is different from the typical 'add to orphan dir'
  2034. * operation in that the inode does not yet exist. This is a problem because
  2035. * the orphan dir stringifies the inode block number to come up with it's
  2036. * dirent. Obviously if the inode does not yet exist we have a chicken and egg
  2037. * problem. This function works around it by calling deeper into the orphan
  2038. * and suballoc code than other callers. Use this only by necessity.
  2039. * @dir: The directory which this inode will ultimately wind up under - not the
  2040. * orphan dir!
  2041. * @dir_bh: buffer_head the @dir inode block
  2042. * @orphan_name: string of length (CFS2_ORPHAN_NAMELEN + 1). Will be filled
  2043. * with the string to be used for orphan dirent. Pass back to the orphan dir
  2044. * code.
  2045. * @ret_orphan_dir: orphan dir inode returned to be passed back into orphan
  2046. * dir code.
  2047. * @ret_di_blkno: block number where the new inode will be allocated.
  2048. * @orphan_insert: Dir insert context to be passed back into orphan dir code.
  2049. * @ret_inode_ac: Inode alloc context to be passed back to the allocator.
  2050. *
  2051. * Returns zero on success and the ret_orphan_dir, name and lookup
  2052. * fields will be populated.
  2053. *
  2054. * Returns non-zero on failure.
  2055. */
  2056. static int ocfs2_prep_new_orphaned_file(struct inode *dir,
  2057. struct buffer_head *dir_bh,
  2058. char *orphan_name,
  2059. struct inode **ret_orphan_dir,
  2060. u64 *ret_di_blkno,
  2061. struct ocfs2_dir_lookup_result *orphan_insert,
  2062. struct ocfs2_alloc_context **ret_inode_ac)
  2063. {
  2064. int ret;
  2065. u64 di_blkno;
  2066. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  2067. struct inode *orphan_dir = NULL;
  2068. struct buffer_head *orphan_dir_bh = NULL;
  2069. struct ocfs2_alloc_context *inode_ac = NULL;
  2070. ret = ocfs2_lookup_lock_orphan_dir(osb, &orphan_dir, &orphan_dir_bh);
  2071. if (ret < 0) {
  2072. mlog_errno(ret);
  2073. return ret;
  2074. }
  2075. /* reserve an inode spot */
  2076. ret = ocfs2_reserve_new_inode(osb, &inode_ac);
  2077. if (ret < 0) {
  2078. if (ret != -ENOSPC)
  2079. mlog_errno(ret);
  2080. goto out;
  2081. }
  2082. ret = ocfs2_find_new_inode_loc(dir, dir_bh, inode_ac,
  2083. &di_blkno);
  2084. if (ret) {
  2085. mlog_errno(ret);
  2086. goto out;
  2087. }
  2088. ret = __ocfs2_prepare_orphan_dir(orphan_dir, orphan_dir_bh,
  2089. di_blkno, orphan_name, orphan_insert,
  2090. false);
  2091. if (ret < 0) {
  2092. mlog_errno(ret);
  2093. goto out;
  2094. }
  2095. out:
  2096. if (ret == 0) {
  2097. *ret_orphan_dir = orphan_dir;
  2098. *ret_di_blkno = di_blkno;
  2099. *ret_inode_ac = inode_ac;
  2100. /*
  2101. * orphan_name and orphan_insert are already up to
  2102. * date via prepare_orphan_dir
  2103. */
  2104. } else {
  2105. /* Unroll reserve_new_inode* */
  2106. if (inode_ac)
  2107. ocfs2_free_alloc_context(inode_ac);
  2108. /* Unroll orphan dir locking */
  2109. mutex_unlock(&orphan_dir->i_mutex);
  2110. ocfs2_inode_unlock(orphan_dir, 1);
  2111. iput(orphan_dir);
  2112. }
  2113. brelse(orphan_dir_bh);
  2114. return ret;
  2115. }
  2116. int ocfs2_create_inode_in_orphan(struct inode *dir,
  2117. int mode,
  2118. struct inode **new_inode)
  2119. {
  2120. int status, did_quota_inode = 0;
  2121. struct inode *inode = NULL;
  2122. struct inode *orphan_dir = NULL;
  2123. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  2124. struct ocfs2_dinode *di = NULL;
  2125. handle_t *handle = NULL;
  2126. char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
  2127. struct buffer_head *parent_di_bh = NULL;
  2128. struct buffer_head *new_di_bh = NULL;
  2129. struct ocfs2_alloc_context *inode_ac = NULL;
  2130. struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
  2131. u64 uninitialized_var(di_blkno), suballoc_loc;
  2132. u16 suballoc_bit;
  2133. status = ocfs2_inode_lock(dir, &parent_di_bh, 1);
  2134. if (status < 0) {
  2135. if (status != -ENOENT)
  2136. mlog_errno(status);
  2137. return status;
  2138. }
  2139. status = ocfs2_prep_new_orphaned_file(dir, parent_di_bh,
  2140. orphan_name, &orphan_dir,
  2141. &di_blkno, &orphan_insert, &inode_ac);
  2142. if (status < 0) {
  2143. if (status != -ENOSPC)
  2144. mlog_errno(status);
  2145. goto leave;
  2146. }
  2147. inode = ocfs2_get_init_inode(dir, mode);
  2148. if (!inode) {
  2149. status = -ENOMEM;
  2150. mlog_errno(status);
  2151. goto leave;
  2152. }
  2153. handle = ocfs2_start_trans(osb, ocfs2_mknod_credits(osb->sb, 0, 0));
  2154. if (IS_ERR(handle)) {
  2155. status = PTR_ERR(handle);
  2156. handle = NULL;
  2157. mlog_errno(status);
  2158. goto leave;
  2159. }
  2160. status = dquot_alloc_inode(inode);
  2161. if (status)
  2162. goto leave;
  2163. did_quota_inode = 1;
  2164. status = ocfs2_claim_new_inode_at_loc(handle, dir, inode_ac,
  2165. &suballoc_loc,
  2166. &suballoc_bit, di_blkno);
  2167. if (status < 0) {
  2168. mlog_errno(status);
  2169. goto leave;
  2170. }
  2171. clear_nlink(inode);
  2172. /* do the real work now. */
  2173. status = __ocfs2_mknod_locked(dir, inode,
  2174. 0, &new_di_bh, parent_di_bh, handle,
  2175. inode_ac, di_blkno, suballoc_loc,
  2176. suballoc_bit);
  2177. if (status < 0) {
  2178. mlog_errno(status);
  2179. goto leave;
  2180. }
  2181. di = (struct ocfs2_dinode *)new_di_bh->b_data;
  2182. status = ocfs2_orphan_add(osb, handle, inode, new_di_bh, orphan_name,
  2183. &orphan_insert, orphan_dir, false);
  2184. if (status < 0) {
  2185. mlog_errno(status);
  2186. goto leave;
  2187. }
  2188. /* get open lock so that only nodes can't remove it from orphan dir. */
  2189. status = ocfs2_open_lock(inode);
  2190. if (status < 0)
  2191. mlog_errno(status);
  2192. insert_inode_hash(inode);
  2193. leave:
  2194. if (status < 0 && did_quota_inode)
  2195. dquot_free_inode(inode);
  2196. if (handle)
  2197. ocfs2_commit_trans(osb, handle);
  2198. if (orphan_dir) {
  2199. /* This was locked for us in ocfs2_prepare_orphan_dir() */
  2200. ocfs2_inode_unlock(orphan_dir, 1);
  2201. mutex_unlock(&orphan_dir->i_mutex);
  2202. iput(orphan_dir);
  2203. }
  2204. if ((status < 0) && inode) {
  2205. clear_nlink(inode);
  2206. iput(inode);
  2207. }
  2208. if (inode_ac)
  2209. ocfs2_free_alloc_context(inode_ac);
  2210. brelse(new_di_bh);
  2211. if (!status)
  2212. *new_inode = inode;
  2213. ocfs2_free_dir_lookup_result(&orphan_insert);
  2214. ocfs2_inode_unlock(dir, 1);
  2215. brelse(parent_di_bh);
  2216. return status;
  2217. }
  2218. static int ocfs2_dio_orphan_recovered(struct inode *inode)
  2219. {
  2220. int ret;
  2221. struct buffer_head *di_bh = NULL;
  2222. struct ocfs2_dinode *di = NULL;
  2223. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  2224. if (ret < 0) {
  2225. mlog_errno(ret);
  2226. return 0;
  2227. }
  2228. di = (struct ocfs2_dinode *) di_bh->b_data;
  2229. ret = !(di->i_flags & cpu_to_le32(OCFS2_DIO_ORPHANED_FL));
  2230. ocfs2_inode_unlock(inode, 1);
  2231. brelse(di_bh);
  2232. return ret;
  2233. }
  2234. #define OCFS2_DIO_ORPHANED_FL_CHECK_INTERVAL 10000
  2235. int ocfs2_add_inode_to_orphan(struct ocfs2_super *osb,
  2236. struct inode *inode)
  2237. {
  2238. char orphan_name[OCFS2_DIO_ORPHAN_PREFIX_LEN + OCFS2_ORPHAN_NAMELEN + 1];
  2239. struct inode *orphan_dir_inode = NULL;
  2240. struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
  2241. struct buffer_head *di_bh = NULL;
  2242. int status = 0;
  2243. handle_t *handle = NULL;
  2244. struct ocfs2_dinode *di = NULL;
  2245. restart:
  2246. status = ocfs2_inode_lock(inode, &di_bh, 1);
  2247. if (status < 0) {
  2248. mlog_errno(status);
  2249. goto bail;
  2250. }
  2251. di = (struct ocfs2_dinode *) di_bh->b_data;
  2252. /*
  2253. * Another append dio crashed?
  2254. * If so, wait for recovery first.
  2255. */
  2256. if (unlikely(di->i_flags & cpu_to_le32(OCFS2_DIO_ORPHANED_FL))) {
  2257. ocfs2_inode_unlock(inode, 1);
  2258. brelse(di_bh);
  2259. wait_event_interruptible_timeout(OCFS2_I(inode)->append_dio_wq,
  2260. ocfs2_dio_orphan_recovered(inode),
  2261. msecs_to_jiffies(OCFS2_DIO_ORPHANED_FL_CHECK_INTERVAL));
  2262. goto restart;
  2263. }
  2264. status = ocfs2_prepare_orphan_dir(osb, &orphan_dir_inode,
  2265. OCFS2_I(inode)->ip_blkno,
  2266. orphan_name,
  2267. &orphan_insert,
  2268. true);
  2269. if (status < 0) {
  2270. mlog_errno(status);
  2271. goto bail_unlock_inode;
  2272. }
  2273. handle = ocfs2_start_trans(osb,
  2274. OCFS2_INODE_ADD_TO_ORPHAN_CREDITS);
  2275. if (IS_ERR(handle)) {
  2276. status = PTR_ERR(handle);
  2277. goto bail_unlock_orphan;
  2278. }
  2279. status = ocfs2_orphan_add(osb, handle, inode, di_bh, orphan_name,
  2280. &orphan_insert, orphan_dir_inode, true);
  2281. if (status)
  2282. mlog_errno(status);
  2283. ocfs2_commit_trans(osb, handle);
  2284. bail_unlock_orphan:
  2285. ocfs2_inode_unlock(orphan_dir_inode, 1);
  2286. mutex_unlock(&orphan_dir_inode->i_mutex);
  2287. iput(orphan_dir_inode);
  2288. ocfs2_free_dir_lookup_result(&orphan_insert);
  2289. bail_unlock_inode:
  2290. ocfs2_inode_unlock(inode, 1);
  2291. brelse(di_bh);
  2292. bail:
  2293. return status;
  2294. }
  2295. int ocfs2_del_inode_from_orphan(struct ocfs2_super *osb,
  2296. struct inode *inode, int update_isize,
  2297. loff_t end)
  2298. {
  2299. struct inode *orphan_dir_inode = NULL;
  2300. struct buffer_head *orphan_dir_bh = NULL;
  2301. struct buffer_head *di_bh = NULL;
  2302. struct ocfs2_dinode *di = NULL;
  2303. handle_t *handle = NULL;
  2304. int status = 0;
  2305. status = ocfs2_inode_lock(inode, &di_bh, 1);
  2306. if (status < 0) {
  2307. mlog_errno(status);
  2308. goto bail;
  2309. }
  2310. di = (struct ocfs2_dinode *) di_bh->b_data;
  2311. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  2312. ORPHAN_DIR_SYSTEM_INODE,
  2313. le16_to_cpu(di->i_dio_orphaned_slot));
  2314. if (!orphan_dir_inode) {
  2315. status = -ENOENT;
  2316. mlog_errno(status);
  2317. goto bail_unlock_inode;
  2318. }
  2319. mutex_lock(&orphan_dir_inode->i_mutex);
  2320. status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
  2321. if (status < 0) {
  2322. mutex_unlock(&orphan_dir_inode->i_mutex);
  2323. iput(orphan_dir_inode);
  2324. mlog_errno(status);
  2325. goto bail_unlock_inode;
  2326. }
  2327. handle = ocfs2_start_trans(osb,
  2328. OCFS2_INODE_DEL_FROM_ORPHAN_CREDITS);
  2329. if (IS_ERR(handle)) {
  2330. status = PTR_ERR(handle);
  2331. goto bail_unlock_orphan;
  2332. }
  2333. BUG_ON(!(di->i_flags & cpu_to_le32(OCFS2_DIO_ORPHANED_FL)));
  2334. status = ocfs2_orphan_del(osb, handle, orphan_dir_inode,
  2335. inode, orphan_dir_bh, true);
  2336. if (status < 0) {
  2337. mlog_errno(status);
  2338. goto bail_commit;
  2339. }
  2340. status = ocfs2_journal_access_di(handle,
  2341. INODE_CACHE(inode),
  2342. di_bh,
  2343. OCFS2_JOURNAL_ACCESS_WRITE);
  2344. if (status < 0) {
  2345. mlog_errno(status);
  2346. goto bail_commit;
  2347. }
  2348. di->i_flags &= ~cpu_to_le32(OCFS2_DIO_ORPHANED_FL);
  2349. di->i_dio_orphaned_slot = 0;
  2350. if (update_isize) {
  2351. status = ocfs2_set_inode_size(handle, inode, di_bh, end);
  2352. if (status)
  2353. mlog_errno(status);
  2354. } else
  2355. ocfs2_journal_dirty(handle, di_bh);
  2356. bail_commit:
  2357. ocfs2_commit_trans(osb, handle);
  2358. bail_unlock_orphan:
  2359. ocfs2_inode_unlock(orphan_dir_inode, 1);
  2360. mutex_unlock(&orphan_dir_inode->i_mutex);
  2361. brelse(orphan_dir_bh);
  2362. iput(orphan_dir_inode);
  2363. bail_unlock_inode:
  2364. ocfs2_inode_unlock(inode, 1);
  2365. brelse(di_bh);
  2366. bail:
  2367. return status;
  2368. }
  2369. int ocfs2_mv_orphaned_inode_to_new(struct inode *dir,
  2370. struct inode *inode,
  2371. struct dentry *dentry)
  2372. {
  2373. int status = 0;
  2374. struct buffer_head *parent_di_bh = NULL;
  2375. handle_t *handle = NULL;
  2376. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  2377. struct ocfs2_dinode *dir_di, *di;
  2378. struct inode *orphan_dir_inode = NULL;
  2379. struct buffer_head *orphan_dir_bh = NULL;
  2380. struct buffer_head *di_bh = NULL;
  2381. struct ocfs2_dir_lookup_result lookup = { NULL, };
  2382. trace_ocfs2_mv_orphaned_inode_to_new(dir, dentry,
  2383. dentry->d_name.len, dentry->d_name.name,
  2384. (unsigned long long)OCFS2_I(dir)->ip_blkno,
  2385. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  2386. status = ocfs2_inode_lock(dir, &parent_di_bh, 1);
  2387. if (status < 0) {
  2388. if (status != -ENOENT)
  2389. mlog_errno(status);
  2390. return status;
  2391. }
  2392. dir_di = (struct ocfs2_dinode *) parent_di_bh->b_data;
  2393. if (!dir_di->i_links_count) {
  2394. /* can't make a file in a deleted directory. */
  2395. status = -ENOENT;
  2396. goto leave;
  2397. }
  2398. status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
  2399. dentry->d_name.len);
  2400. if (status)
  2401. goto leave;
  2402. /* get a spot inside the dir. */
  2403. status = ocfs2_prepare_dir_for_insert(osb, dir, parent_di_bh,
  2404. dentry->d_name.name,
  2405. dentry->d_name.len, &lookup);
  2406. if (status < 0) {
  2407. mlog_errno(status);
  2408. goto leave;
  2409. }
  2410. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  2411. ORPHAN_DIR_SYSTEM_INODE,
  2412. osb->slot_num);
  2413. if (!orphan_dir_inode) {
  2414. status = -ENOENT;
  2415. mlog_errno(status);
  2416. goto leave;
  2417. }
  2418. mutex_lock(&orphan_dir_inode->i_mutex);
  2419. status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
  2420. if (status < 0) {
  2421. mlog_errno(status);
  2422. mutex_unlock(&orphan_dir_inode->i_mutex);
  2423. iput(orphan_dir_inode);
  2424. goto leave;
  2425. }
  2426. status = ocfs2_read_inode_block(inode, &di_bh);
  2427. if (status < 0) {
  2428. mlog_errno(status);
  2429. goto orphan_unlock;
  2430. }
  2431. handle = ocfs2_start_trans(osb, ocfs2_rename_credits(osb->sb));
  2432. if (IS_ERR(handle)) {
  2433. status = PTR_ERR(handle);
  2434. handle = NULL;
  2435. mlog_errno(status);
  2436. goto orphan_unlock;
  2437. }
  2438. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
  2439. di_bh, OCFS2_JOURNAL_ACCESS_WRITE);
  2440. if (status < 0) {
  2441. mlog_errno(status);
  2442. goto out_commit;
  2443. }
  2444. status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode,
  2445. orphan_dir_bh, false);
  2446. if (status < 0) {
  2447. mlog_errno(status);
  2448. goto out_commit;
  2449. }
  2450. di = (struct ocfs2_dinode *)di_bh->b_data;
  2451. di->i_flags &= ~cpu_to_le32(OCFS2_ORPHANED_FL);
  2452. di->i_orphaned_slot = 0;
  2453. set_nlink(inode, 1);
  2454. ocfs2_set_links_count(di, inode->i_nlink);
  2455. ocfs2_update_inode_fsync_trans(handle, inode, 1);
  2456. ocfs2_journal_dirty(handle, di_bh);
  2457. status = ocfs2_add_entry(handle, dentry, inode,
  2458. OCFS2_I(inode)->ip_blkno, parent_di_bh,
  2459. &lookup);
  2460. if (status < 0) {
  2461. mlog_errno(status);
  2462. goto out_commit;
  2463. }
  2464. status = ocfs2_dentry_attach_lock(dentry, inode,
  2465. OCFS2_I(dir)->ip_blkno);
  2466. if (status) {
  2467. mlog_errno(status);
  2468. goto out_commit;
  2469. }
  2470. d_instantiate(dentry, inode);
  2471. status = 0;
  2472. out_commit:
  2473. ocfs2_commit_trans(osb, handle);
  2474. orphan_unlock:
  2475. ocfs2_inode_unlock(orphan_dir_inode, 1);
  2476. mutex_unlock(&orphan_dir_inode->i_mutex);
  2477. iput(orphan_dir_inode);
  2478. leave:
  2479. ocfs2_inode_unlock(dir, 1);
  2480. brelse(di_bh);
  2481. brelse(parent_di_bh);
  2482. brelse(orphan_dir_bh);
  2483. ocfs2_free_dir_lookup_result(&lookup);
  2484. if (status)
  2485. mlog_errno(status);
  2486. return status;
  2487. }
  2488. const struct inode_operations ocfs2_dir_iops = {
  2489. .create = ocfs2_create,
  2490. .lookup = ocfs2_lookup,
  2491. .link = ocfs2_link,
  2492. .unlink = ocfs2_unlink,
  2493. .rmdir = ocfs2_unlink,
  2494. .symlink = ocfs2_symlink,
  2495. .mkdir = ocfs2_mkdir,
  2496. .mknod = ocfs2_mknod,
  2497. .rename = ocfs2_rename,
  2498. .setattr = ocfs2_setattr,
  2499. .getattr = ocfs2_getattr,
  2500. .permission = ocfs2_permission,
  2501. .setxattr = generic_setxattr,
  2502. .getxattr = generic_getxattr,
  2503. .listxattr = ocfs2_listxattr,
  2504. .removexattr = generic_removexattr,
  2505. .fiemap = ocfs2_fiemap,
  2506. .get_acl = ocfs2_iop_get_acl,
  2507. .set_acl = ocfs2_iop_set_acl,
  2508. };