namei.c 64 KB

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