namei.c 70 KB

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