namei.c 56 KB

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