namei.c 69 KB

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