namei.c 70 KB

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