namei.c 70 KB

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