namei.c 55 KB

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