namei.c 65 KB

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