namei.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052
  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. struct dentry *dentry,
  67. dev_t dev,
  68. struct buffer_head **new_fe_bh,
  69. struct buffer_head *parent_fe_bh,
  70. handle_t *handle,
  71. struct ocfs2_alloc_context *inode_ac);
  72. static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
  73. struct inode **ret_orphan_dir,
  74. struct inode *inode,
  75. char *name,
  76. struct ocfs2_dir_lookup_result *lookup);
  77. static int ocfs2_orphan_add(struct ocfs2_super *osb,
  78. handle_t *handle,
  79. struct inode *inode,
  80. struct ocfs2_dinode *fe,
  81. char *name,
  82. struct ocfs2_dir_lookup_result *lookup,
  83. struct inode *orphan_dir_inode);
  84. static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
  85. handle_t *handle,
  86. struct inode *inode,
  87. const char *symname);
  88. /* An orphan dir name is an 8 byte value, printed as a hex string */
  89. #define OCFS2_ORPHAN_NAMELEN ((int)(2 * sizeof(u64)))
  90. static struct dentry *ocfs2_lookup(struct inode *dir, struct dentry *dentry,
  91. struct nameidata *nd)
  92. {
  93. int status;
  94. u64 blkno;
  95. struct inode *inode = NULL;
  96. struct dentry *ret;
  97. struct ocfs2_inode_info *oi;
  98. mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
  99. dentry->d_name.len, dentry->d_name.name);
  100. if (dentry->d_name.len > OCFS2_MAX_FILENAME_LEN) {
  101. ret = ERR_PTR(-ENAMETOOLONG);
  102. goto bail;
  103. }
  104. mlog(0, "find name %.*s in directory %llu\n", dentry->d_name.len,
  105. dentry->d_name.name, (unsigned long long)OCFS2_I(dir)->ip_blkno);
  106. status = ocfs2_inode_lock(dir, NULL, 0);
  107. if (status < 0) {
  108. if (status != -ENOENT)
  109. mlog_errno(status);
  110. ret = ERR_PTR(status);
  111. goto bail;
  112. }
  113. status = ocfs2_lookup_ino_from_name(dir, dentry->d_name.name,
  114. dentry->d_name.len, &blkno);
  115. if (status < 0)
  116. goto bail_add;
  117. inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0, 0);
  118. if (IS_ERR(inode)) {
  119. ret = ERR_PTR(-EACCES);
  120. goto bail_unlock;
  121. }
  122. oi = OCFS2_I(inode);
  123. /* Clear any orphaned state... If we were able to look up the
  124. * inode from a directory, it certainly can't be orphaned. We
  125. * might have the bad state from a node which intended to
  126. * orphan this inode but crashed before it could commit the
  127. * unlink. */
  128. spin_lock(&oi->ip_lock);
  129. oi->ip_flags &= ~OCFS2_INODE_MAYBE_ORPHANED;
  130. spin_unlock(&oi->ip_lock);
  131. bail_add:
  132. dentry->d_op = &ocfs2_dentry_ops;
  133. ret = d_splice_alias(inode, dentry);
  134. if (inode) {
  135. /*
  136. * If d_splice_alias() finds a DCACHE_DISCONNECTED
  137. * dentry, it will d_move() it on top of ourse. The
  138. * return value will indicate this however, so in
  139. * those cases, we switch them around for the locking
  140. * code.
  141. *
  142. * NOTE: This dentry already has ->d_op set from
  143. * ocfs2_get_parent() and ocfs2_get_dentry()
  144. */
  145. if (ret)
  146. dentry = ret;
  147. status = ocfs2_dentry_attach_lock(dentry, inode,
  148. OCFS2_I(dir)->ip_blkno);
  149. if (status) {
  150. mlog_errno(status);
  151. ret = ERR_PTR(status);
  152. goto bail_unlock;
  153. }
  154. }
  155. bail_unlock:
  156. /* Don't drop the cluster lock until *after* the d_add --
  157. * unlink on another node will message us to remove that
  158. * dentry under this lock so otherwise we can race this with
  159. * the downconvert thread and have a stale dentry. */
  160. ocfs2_inode_unlock(dir, 0);
  161. bail:
  162. mlog_exit_ptr(ret);
  163. return ret;
  164. }
  165. static struct inode *ocfs2_get_init_inode(struct inode *dir, int mode)
  166. {
  167. struct inode *inode;
  168. inode = new_inode(dir->i_sb);
  169. if (!inode) {
  170. mlog(ML_ERROR, "new_inode failed!\n");
  171. return NULL;
  172. }
  173. /* populate as many fields early on as possible - many of
  174. * these are used by the support functions here and in
  175. * callers. */
  176. if (S_ISDIR(mode))
  177. inode->i_nlink = 2;
  178. else
  179. inode->i_nlink = 1;
  180. inode->i_uid = current_fsuid();
  181. if (dir->i_mode & S_ISGID) {
  182. inode->i_gid = dir->i_gid;
  183. if (S_ISDIR(mode))
  184. mode |= S_ISGID;
  185. } else
  186. inode->i_gid = current_fsgid();
  187. inode->i_mode = mode;
  188. vfs_dq_init(inode);
  189. return inode;
  190. }
  191. static int ocfs2_mknod(struct inode *dir,
  192. struct dentry *dentry,
  193. int mode,
  194. dev_t dev)
  195. {
  196. int status = 0;
  197. struct buffer_head *parent_fe_bh = NULL;
  198. handle_t *handle = NULL;
  199. struct ocfs2_super *osb;
  200. struct ocfs2_dinode *dirfe;
  201. struct buffer_head *new_fe_bh = NULL;
  202. struct inode *inode = NULL;
  203. struct ocfs2_alloc_context *inode_ac = NULL;
  204. struct ocfs2_alloc_context *data_ac = NULL;
  205. struct ocfs2_alloc_context *meta_ac = NULL;
  206. int want_clusters = 0;
  207. int want_meta = 0;
  208. int xattr_credits = 0;
  209. struct ocfs2_security_xattr_info si = {
  210. .enable = 1,
  211. };
  212. int did_quota_inode = 0;
  213. struct ocfs2_dir_lookup_result lookup = { NULL, };
  214. mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
  215. (unsigned long)dev, dentry->d_name.len,
  216. dentry->d_name.name);
  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)) {
  226. status = -EMLINK;
  227. goto leave;
  228. }
  229. dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
  230. if (!dirfe->i_links_count) {
  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. /* We don't use standard VFS wrapper because we don't want vfs_dq_init
  307. * to be called. */
  308. if (sb_any_quota_active(osb->sb) &&
  309. osb->sb->dq_op->alloc_inode(inode, 1) == NO_QUOTA) {
  310. status = -EDQUOT;
  311. goto leave;
  312. }
  313. did_quota_inode = 1;
  314. /* do the real work now. */
  315. status = ocfs2_mknod_locked(osb, dir, inode, dentry, dev,
  316. &new_fe_bh, parent_fe_bh, handle,
  317. inode_ac);
  318. if (status < 0) {
  319. mlog_errno(status);
  320. goto leave;
  321. }
  322. if (S_ISDIR(mode)) {
  323. status = ocfs2_fill_new_dir(osb, handle, dir, inode,
  324. new_fe_bh, data_ac, meta_ac);
  325. if (status < 0) {
  326. mlog_errno(status);
  327. goto leave;
  328. }
  329. status = ocfs2_journal_access_di(handle, dir, parent_fe_bh,
  330. OCFS2_JOURNAL_ACCESS_WRITE);
  331. if (status < 0) {
  332. mlog_errno(status);
  333. goto leave;
  334. }
  335. le16_add_cpu(&dirfe->i_links_count, 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. status = ocfs2_add_entry(handle, dentry, inode,
  358. OCFS2_I(inode)->ip_blkno, parent_fe_bh,
  359. &lookup);
  360. if (status < 0) {
  361. mlog_errno(status);
  362. goto leave;
  363. }
  364. status = ocfs2_dentry_attach_lock(dentry, inode,
  365. OCFS2_I(dir)->ip_blkno);
  366. if (status) {
  367. mlog_errno(status);
  368. goto leave;
  369. }
  370. insert_inode_hash(inode);
  371. dentry->d_op = &ocfs2_dentry_ops;
  372. d_instantiate(dentry, inode);
  373. status = 0;
  374. leave:
  375. if (status < 0 && did_quota_inode)
  376. vfs_dq_free_inode(inode);
  377. if (handle)
  378. ocfs2_commit_trans(osb, handle);
  379. ocfs2_inode_unlock(dir, 1);
  380. if (status == -ENOSPC)
  381. mlog(0, "Disk is full\n");
  382. brelse(new_fe_bh);
  383. brelse(parent_fe_bh);
  384. kfree(si.name);
  385. kfree(si.value);
  386. ocfs2_free_dir_lookup_result(&lookup);
  387. if ((status < 0) && inode) {
  388. clear_nlink(inode);
  389. iput(inode);
  390. }
  391. if (inode_ac)
  392. ocfs2_free_alloc_context(inode_ac);
  393. if (data_ac)
  394. ocfs2_free_alloc_context(data_ac);
  395. if (meta_ac)
  396. ocfs2_free_alloc_context(meta_ac);
  397. mlog_exit(status);
  398. return status;
  399. }
  400. static int ocfs2_mknod_locked(struct ocfs2_super *osb,
  401. struct inode *dir,
  402. struct inode *inode,
  403. struct dentry *dentry,
  404. dev_t dev,
  405. struct buffer_head **new_fe_bh,
  406. struct buffer_head *parent_fe_bh,
  407. handle_t *handle,
  408. struct ocfs2_alloc_context *inode_ac)
  409. {
  410. int status = 0;
  411. struct ocfs2_dinode *fe = NULL;
  412. struct ocfs2_extent_list *fel;
  413. u64 fe_blkno = 0;
  414. u16 suballoc_bit;
  415. u16 feat;
  416. mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry,
  417. inode->i_mode, (unsigned long)dev, dentry->d_name.len,
  418. dentry->d_name.name);
  419. *new_fe_bh = NULL;
  420. status = ocfs2_claim_new_inode(osb, handle, inode_ac, &suballoc_bit,
  421. &fe_blkno);
  422. if (status < 0) {
  423. mlog_errno(status);
  424. goto leave;
  425. }
  426. /* populate as many fields early on as possible - many of
  427. * these are used by the support functions here and in
  428. * callers. */
  429. inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
  430. OCFS2_I(inode)->ip_blkno = fe_blkno;
  431. spin_lock(&osb->osb_lock);
  432. inode->i_generation = osb->s_next_generation++;
  433. spin_unlock(&osb->osb_lock);
  434. *new_fe_bh = sb_getblk(osb->sb, fe_blkno);
  435. if (!*new_fe_bh) {
  436. status = -EIO;
  437. mlog_errno(status);
  438. goto leave;
  439. }
  440. ocfs2_set_new_buffer_uptodate(inode, *new_fe_bh);
  441. status = ocfs2_journal_access_di(handle, inode, *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. fe->i_links_count = cpu_to_le16(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. status = ocfs2_journal_dirty(handle, *new_fe_bh);
  484. if (status < 0) {
  485. mlog_errno(status);
  486. goto leave;
  487. }
  488. ocfs2_populate_inode(inode, fe, 1);
  489. ocfs2_inode_set_new(osb, inode);
  490. if (!ocfs2_mount_local(osb)) {
  491. status = ocfs2_create_new_inode_locks(inode);
  492. if (status < 0)
  493. mlog_errno(status);
  494. }
  495. status = 0; /* error in ocfs2_create_new_inode_locks is not
  496. * critical */
  497. leave:
  498. if (status < 0) {
  499. if (*new_fe_bh) {
  500. brelse(*new_fe_bh);
  501. *new_fe_bh = NULL;
  502. }
  503. }
  504. mlog_exit(status);
  505. return status;
  506. }
  507. static int ocfs2_mkdir(struct inode *dir,
  508. struct dentry *dentry,
  509. int mode)
  510. {
  511. int ret;
  512. mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
  513. dentry->d_name.len, dentry->d_name.name);
  514. ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
  515. mlog_exit(ret);
  516. return ret;
  517. }
  518. static int ocfs2_create(struct inode *dir,
  519. struct dentry *dentry,
  520. int mode,
  521. struct nameidata *nd)
  522. {
  523. int ret;
  524. mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
  525. dentry->d_name.len, dentry->d_name.name);
  526. ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0);
  527. mlog_exit(ret);
  528. return ret;
  529. }
  530. static int ocfs2_link(struct dentry *old_dentry,
  531. struct inode *dir,
  532. struct dentry *dentry)
  533. {
  534. handle_t *handle;
  535. struct inode *inode = old_dentry->d_inode;
  536. int err;
  537. struct buffer_head *fe_bh = NULL;
  538. struct buffer_head *parent_fe_bh = NULL;
  539. struct ocfs2_dinode *fe = NULL;
  540. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  541. struct ocfs2_dir_lookup_result lookup = { NULL, };
  542. mlog_entry("(inode=%lu, old='%.*s' new='%.*s')\n", inode->i_ino,
  543. old_dentry->d_name.len, old_dentry->d_name.name,
  544. dentry->d_name.len, dentry->d_name.name);
  545. if (S_ISDIR(inode->i_mode))
  546. return -EPERM;
  547. err = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
  548. if (err < 0) {
  549. if (err != -ENOENT)
  550. mlog_errno(err);
  551. return err;
  552. }
  553. if (!dir->i_nlink) {
  554. err = -ENOENT;
  555. goto out;
  556. }
  557. err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
  558. dentry->d_name.len);
  559. if (err)
  560. goto out;
  561. err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
  562. dentry->d_name.name,
  563. dentry->d_name.len, &lookup);
  564. if (err < 0) {
  565. mlog_errno(err);
  566. goto out;
  567. }
  568. err = ocfs2_inode_lock(inode, &fe_bh, 1);
  569. if (err < 0) {
  570. if (err != -ENOENT)
  571. mlog_errno(err);
  572. goto out;
  573. }
  574. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  575. if (le16_to_cpu(fe->i_links_count) >= OCFS2_LINK_MAX) {
  576. err = -EMLINK;
  577. goto out_unlock_inode;
  578. }
  579. handle = ocfs2_start_trans(osb, ocfs2_link_credits(osb->sb));
  580. if (IS_ERR(handle)) {
  581. err = PTR_ERR(handle);
  582. handle = NULL;
  583. mlog_errno(err);
  584. goto out_unlock_inode;
  585. }
  586. err = ocfs2_journal_access_di(handle, 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. fe->i_links_count = cpu_to_le16(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. err = ocfs2_journal_dirty(handle, fe_bh);
  598. if (err < 0) {
  599. le16_add_cpu(&fe->i_links_count, -1);
  600. drop_nlink(inode);
  601. mlog_errno(err);
  602. goto out_commit;
  603. }
  604. err = ocfs2_add_entry(handle, dentry, inode,
  605. OCFS2_I(inode)->ip_blkno,
  606. parent_fe_bh, &lookup);
  607. if (err) {
  608. le16_add_cpu(&fe->i_links_count, -1);
  609. drop_nlink(inode);
  610. mlog_errno(err);
  611. goto out_commit;
  612. }
  613. err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
  614. if (err) {
  615. mlog_errno(err);
  616. goto out_commit;
  617. }
  618. atomic_inc(&inode->i_count);
  619. dentry->d_op = &ocfs2_dentry_ops;
  620. d_instantiate(dentry, inode);
  621. out_commit:
  622. ocfs2_commit_trans(osb, handle);
  623. out_unlock_inode:
  624. ocfs2_inode_unlock(inode, 1);
  625. out:
  626. ocfs2_inode_unlock(dir, 1);
  627. brelse(fe_bh);
  628. brelse(parent_fe_bh);
  629. ocfs2_free_dir_lookup_result(&lookup);
  630. mlog_exit(err);
  631. return err;
  632. }
  633. /*
  634. * Takes and drops an exclusive lock on the given dentry. This will
  635. * force other nodes to drop it.
  636. */
  637. static int ocfs2_remote_dentry_delete(struct dentry *dentry)
  638. {
  639. int ret;
  640. ret = ocfs2_dentry_lock(dentry, 1);
  641. if (ret)
  642. mlog_errno(ret);
  643. else
  644. ocfs2_dentry_unlock(dentry, 1);
  645. return ret;
  646. }
  647. static inline int inode_is_unlinkable(struct inode *inode)
  648. {
  649. if (S_ISDIR(inode->i_mode)) {
  650. if (inode->i_nlink == 2)
  651. return 1;
  652. return 0;
  653. }
  654. if (inode->i_nlink == 1)
  655. return 1;
  656. return 0;
  657. }
  658. static int ocfs2_unlink(struct inode *dir,
  659. struct dentry *dentry)
  660. {
  661. int status;
  662. int child_locked = 0;
  663. struct inode *inode = dentry->d_inode;
  664. struct inode *orphan_dir = NULL;
  665. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  666. u64 blkno;
  667. struct ocfs2_dinode *fe = NULL;
  668. struct buffer_head *fe_bh = NULL;
  669. struct buffer_head *parent_node_bh = NULL;
  670. handle_t *handle = NULL;
  671. char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
  672. struct ocfs2_dir_lookup_result lookup = { NULL, };
  673. struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
  674. mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
  675. dentry->d_name.len, dentry->d_name.name);
  676. BUG_ON(dentry->d_parent->d_inode != dir);
  677. mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
  678. if (inode == osb->root_inode) {
  679. mlog(0, "Cannot delete the root directory\n");
  680. return -EPERM;
  681. }
  682. status = ocfs2_inode_lock(dir, &parent_node_bh, 1);
  683. if (status < 0) {
  684. if (status != -ENOENT)
  685. mlog_errno(status);
  686. return status;
  687. }
  688. status = ocfs2_find_files_on_disk(dentry->d_name.name,
  689. dentry->d_name.len, &blkno, dir,
  690. &lookup);
  691. if (status < 0) {
  692. if (status != -ENOENT)
  693. mlog_errno(status);
  694. goto leave;
  695. }
  696. if (OCFS2_I(inode)->ip_blkno != blkno) {
  697. status = -ENOENT;
  698. mlog(0, "ip_blkno %llu != dirent blkno %llu ip_flags = %x\n",
  699. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  700. (unsigned long long)blkno, OCFS2_I(inode)->ip_flags);
  701. goto leave;
  702. }
  703. status = ocfs2_inode_lock(inode, &fe_bh, 1);
  704. if (status < 0) {
  705. if (status != -ENOENT)
  706. mlog_errno(status);
  707. goto leave;
  708. }
  709. child_locked = 1;
  710. if (S_ISDIR(inode->i_mode)) {
  711. if (!ocfs2_empty_dir(inode)) {
  712. status = -ENOTEMPTY;
  713. goto leave;
  714. } else if (inode->i_nlink != 2) {
  715. status = -ENOTEMPTY;
  716. goto leave;
  717. }
  718. }
  719. status = ocfs2_remote_dentry_delete(dentry);
  720. if (status < 0) {
  721. /* This remote delete should succeed under all normal
  722. * circumstances. */
  723. mlog_errno(status);
  724. goto leave;
  725. }
  726. if (inode_is_unlinkable(inode)) {
  727. status = ocfs2_prepare_orphan_dir(osb, &orphan_dir, inode,
  728. orphan_name, &orphan_insert);
  729. if (status < 0) {
  730. mlog_errno(status);
  731. goto leave;
  732. }
  733. }
  734. handle = ocfs2_start_trans(osb, ocfs2_unlink_credits(osb->sb));
  735. if (IS_ERR(handle)) {
  736. status = PTR_ERR(handle);
  737. handle = NULL;
  738. mlog_errno(status);
  739. goto leave;
  740. }
  741. status = ocfs2_journal_access_di(handle, inode, fe_bh,
  742. OCFS2_JOURNAL_ACCESS_WRITE);
  743. if (status < 0) {
  744. mlog_errno(status);
  745. goto leave;
  746. }
  747. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  748. if (inode_is_unlinkable(inode)) {
  749. status = ocfs2_orphan_add(osb, handle, inode, fe, orphan_name,
  750. &orphan_insert, orphan_dir);
  751. if (status < 0) {
  752. mlog_errno(status);
  753. goto leave;
  754. }
  755. }
  756. /* delete the name from the parent dir */
  757. status = ocfs2_delete_entry(handle, dir, &lookup);
  758. if (status < 0) {
  759. mlog_errno(status);
  760. goto leave;
  761. }
  762. if (S_ISDIR(inode->i_mode))
  763. drop_nlink(inode);
  764. drop_nlink(inode);
  765. fe->i_links_count = cpu_to_le16(inode->i_nlink);
  766. status = ocfs2_journal_dirty(handle, fe_bh);
  767. if (status < 0) {
  768. mlog_errno(status);
  769. goto leave;
  770. }
  771. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  772. if (S_ISDIR(inode->i_mode))
  773. drop_nlink(dir);
  774. status = ocfs2_mark_inode_dirty(handle, dir, parent_node_bh);
  775. if (status < 0) {
  776. mlog_errno(status);
  777. if (S_ISDIR(inode->i_mode))
  778. inc_nlink(dir);
  779. }
  780. leave:
  781. if (handle)
  782. ocfs2_commit_trans(osb, handle);
  783. if (child_locked)
  784. ocfs2_inode_unlock(inode, 1);
  785. ocfs2_inode_unlock(dir, 1);
  786. if (orphan_dir) {
  787. /* This was locked for us in ocfs2_prepare_orphan_dir() */
  788. ocfs2_inode_unlock(orphan_dir, 1);
  789. mutex_unlock(&orphan_dir->i_mutex);
  790. iput(orphan_dir);
  791. }
  792. brelse(fe_bh);
  793. brelse(parent_node_bh);
  794. ocfs2_free_dir_lookup_result(&orphan_insert);
  795. ocfs2_free_dir_lookup_result(&lookup);
  796. mlog_exit(status);
  797. return status;
  798. }
  799. /*
  800. * The only place this should be used is rename!
  801. * if they have the same id, then the 1st one is the only one locked.
  802. */
  803. static int ocfs2_double_lock(struct ocfs2_super *osb,
  804. struct buffer_head **bh1,
  805. struct inode *inode1,
  806. struct buffer_head **bh2,
  807. struct inode *inode2)
  808. {
  809. int status;
  810. struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
  811. struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
  812. struct buffer_head **tmpbh;
  813. struct inode *tmpinode;
  814. mlog_entry("(inode1 = %llu, inode2 = %llu)\n",
  815. (unsigned long long)oi1->ip_blkno,
  816. (unsigned long long)oi2->ip_blkno);
  817. if (*bh1)
  818. *bh1 = NULL;
  819. if (*bh2)
  820. *bh2 = NULL;
  821. /* we always want to lock the one with the lower lockid first. */
  822. if (oi1->ip_blkno != oi2->ip_blkno) {
  823. if (oi1->ip_blkno < oi2->ip_blkno) {
  824. /* switch id1 and id2 around */
  825. mlog(0, "switching them around...\n");
  826. tmpbh = bh2;
  827. bh2 = bh1;
  828. bh1 = tmpbh;
  829. tmpinode = inode2;
  830. inode2 = inode1;
  831. inode1 = tmpinode;
  832. }
  833. /* lock id2 */
  834. status = ocfs2_inode_lock(inode2, bh2, 1);
  835. if (status < 0) {
  836. if (status != -ENOENT)
  837. mlog_errno(status);
  838. goto bail;
  839. }
  840. }
  841. /* lock id1 */
  842. status = ocfs2_inode_lock(inode1, bh1, 1);
  843. if (status < 0) {
  844. /*
  845. * An error return must mean that no cluster locks
  846. * were held on function exit.
  847. */
  848. if (oi1->ip_blkno != oi2->ip_blkno)
  849. ocfs2_inode_unlock(inode2, 1);
  850. if (status != -ENOENT)
  851. mlog_errno(status);
  852. }
  853. bail:
  854. mlog_exit(status);
  855. return status;
  856. }
  857. static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2)
  858. {
  859. ocfs2_inode_unlock(inode1, 1);
  860. if (inode1 != inode2)
  861. ocfs2_inode_unlock(inode2, 1);
  862. }
  863. static int ocfs2_rename(struct inode *old_dir,
  864. struct dentry *old_dentry,
  865. struct inode *new_dir,
  866. struct dentry *new_dentry)
  867. {
  868. int status = 0, rename_lock = 0, parents_locked = 0, target_exists = 0;
  869. int old_child_locked = 0, new_child_locked = 0, update_dot_dot = 0;
  870. struct inode *old_inode = old_dentry->d_inode;
  871. struct inode *new_inode = new_dentry->d_inode;
  872. struct inode *orphan_dir = NULL;
  873. struct ocfs2_dinode *newfe = NULL;
  874. char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
  875. struct buffer_head *orphan_entry_bh = NULL;
  876. struct buffer_head *newfe_bh = NULL;
  877. struct buffer_head *old_inode_bh = NULL;
  878. struct buffer_head *insert_entry_bh = NULL;
  879. struct ocfs2_super *osb = NULL;
  880. u64 newfe_blkno, old_de_ino;
  881. handle_t *handle = NULL;
  882. struct buffer_head *old_dir_bh = NULL;
  883. struct buffer_head *new_dir_bh = NULL;
  884. nlink_t old_dir_nlink = old_dir->i_nlink;
  885. struct ocfs2_dinode *old_di;
  886. struct ocfs2_dir_lookup_result old_inode_dot_dot_res = { NULL, };
  887. struct ocfs2_dir_lookup_result target_lookup_res = { NULL, };
  888. struct ocfs2_dir_lookup_result old_entry_lookup = { NULL, };
  889. struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
  890. struct ocfs2_dir_lookup_result target_insert = { NULL, };
  891. /* At some point it might be nice to break this function up a
  892. * bit. */
  893. mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p, from='%.*s' to='%.*s')\n",
  894. old_dir, old_dentry, new_dir, new_dentry,
  895. old_dentry->d_name.len, old_dentry->d_name.name,
  896. new_dentry->d_name.len, new_dentry->d_name.name);
  897. osb = OCFS2_SB(old_dir->i_sb);
  898. if (new_inode) {
  899. if (!igrab(new_inode))
  900. BUG();
  901. }
  902. /* Assume a directory hierarchy thusly:
  903. * a/b/c
  904. * a/d
  905. * a,b,c, and d are all directories.
  906. *
  907. * from cwd of 'a' on both nodes:
  908. * node1: mv b/c d
  909. * node2: mv d b/c
  910. *
  911. * And that's why, just like the VFS, we need a file system
  912. * rename lock. */
  913. if (old_dir != new_dir && S_ISDIR(old_inode->i_mode)) {
  914. status = ocfs2_rename_lock(osb);
  915. if (status < 0) {
  916. mlog_errno(status);
  917. goto bail;
  918. }
  919. rename_lock = 1;
  920. }
  921. /* if old and new are the same, this'll just do one lock. */
  922. status = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
  923. &new_dir_bh, new_dir);
  924. if (status < 0) {
  925. mlog_errno(status);
  926. goto bail;
  927. }
  928. parents_locked = 1;
  929. /* make sure both dirs have bhs
  930. * get an extra ref on old_dir_bh if old==new */
  931. if (!new_dir_bh) {
  932. if (old_dir_bh) {
  933. new_dir_bh = old_dir_bh;
  934. get_bh(new_dir_bh);
  935. } else {
  936. mlog(ML_ERROR, "no old_dir_bh!\n");
  937. status = -EIO;
  938. goto bail;
  939. }
  940. }
  941. /*
  942. * Aside from allowing a meta data update, the locking here
  943. * also ensures that the downconvert thread on other nodes
  944. * won't have to concurrently downconvert the inode and the
  945. * dentry locks.
  946. */
  947. status = ocfs2_inode_lock(old_inode, &old_inode_bh, 1);
  948. if (status < 0) {
  949. if (status != -ENOENT)
  950. mlog_errno(status);
  951. goto bail;
  952. }
  953. old_child_locked = 1;
  954. status = ocfs2_remote_dentry_delete(old_dentry);
  955. if (status < 0) {
  956. mlog_errno(status);
  957. goto bail;
  958. }
  959. if (S_ISDIR(old_inode->i_mode)) {
  960. u64 old_inode_parent;
  961. update_dot_dot = 1;
  962. status = ocfs2_find_files_on_disk("..", 2, &old_inode_parent,
  963. old_inode,
  964. &old_inode_dot_dot_res);
  965. if (status) {
  966. status = -EIO;
  967. goto bail;
  968. }
  969. if (old_inode_parent != OCFS2_I(old_dir)->ip_blkno) {
  970. status = -EIO;
  971. goto bail;
  972. }
  973. if (!new_inode && new_dir != old_dir &&
  974. new_dir->i_nlink >= OCFS2_LINK_MAX) {
  975. status = -EMLINK;
  976. goto bail;
  977. }
  978. }
  979. status = ocfs2_lookup_ino_from_name(old_dir, old_dentry->d_name.name,
  980. old_dentry->d_name.len,
  981. &old_de_ino);
  982. if (status) {
  983. status = -ENOENT;
  984. goto bail;
  985. }
  986. /*
  987. * Check for inode number is _not_ due to possible IO errors.
  988. * We might rmdir the source, keep it as pwd of some process
  989. * and merrily kill the link to whatever was created under the
  990. * same name. Goodbye sticky bit ;-<
  991. */
  992. if (old_de_ino != OCFS2_I(old_inode)->ip_blkno) {
  993. status = -ENOENT;
  994. goto bail;
  995. }
  996. /* check if the target already exists (in which case we need
  997. * to delete it */
  998. status = ocfs2_find_files_on_disk(new_dentry->d_name.name,
  999. new_dentry->d_name.len,
  1000. &newfe_blkno, new_dir,
  1001. &target_lookup_res);
  1002. /* The only error we allow here is -ENOENT because the new
  1003. * file not existing is perfectly valid. */
  1004. if ((status < 0) && (status != -ENOENT)) {
  1005. /* If we cannot find the file specified we should just */
  1006. /* return the error... */
  1007. mlog_errno(status);
  1008. goto bail;
  1009. }
  1010. if (status == 0)
  1011. target_exists = 1;
  1012. if (!target_exists && new_inode) {
  1013. /*
  1014. * Target was unlinked by another node while we were
  1015. * waiting to get to ocfs2_rename(). There isn't
  1016. * anything we can do here to help the situation, so
  1017. * bubble up the appropriate error.
  1018. */
  1019. status = -ENOENT;
  1020. goto bail;
  1021. }
  1022. /* In case we need to overwrite an existing file, we blow it
  1023. * away first */
  1024. if (target_exists) {
  1025. /* VFS didn't think there existed an inode here, but
  1026. * someone else in the cluster must have raced our
  1027. * rename to create one. Today we error cleanly, in
  1028. * the future we should consider calling iget to build
  1029. * a new struct inode for this entry. */
  1030. if (!new_inode) {
  1031. status = -EACCES;
  1032. mlog(0, "We found an inode for name %.*s but VFS "
  1033. "didn't give us one.\n", new_dentry->d_name.len,
  1034. new_dentry->d_name.name);
  1035. goto bail;
  1036. }
  1037. if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
  1038. status = -EACCES;
  1039. mlog(0, "Inode %llu and dir %llu disagree. flags = %x\n",
  1040. (unsigned long long)OCFS2_I(new_inode)->ip_blkno,
  1041. (unsigned long long)newfe_blkno,
  1042. OCFS2_I(new_inode)->ip_flags);
  1043. goto bail;
  1044. }
  1045. status = ocfs2_inode_lock(new_inode, &newfe_bh, 1);
  1046. if (status < 0) {
  1047. if (status != -ENOENT)
  1048. mlog_errno(status);
  1049. goto bail;
  1050. }
  1051. new_child_locked = 1;
  1052. status = ocfs2_remote_dentry_delete(new_dentry);
  1053. if (status < 0) {
  1054. mlog_errno(status);
  1055. goto bail;
  1056. }
  1057. newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
  1058. mlog(0, "aha rename over existing... new_blkno=%llu "
  1059. "newfebh=%p bhblocknr=%llu\n",
  1060. (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
  1061. (unsigned long long)newfe_bh->b_blocknr : 0ULL);
  1062. if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
  1063. status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
  1064. new_inode,
  1065. orphan_name,
  1066. &orphan_insert);
  1067. if (status < 0) {
  1068. mlog_errno(status);
  1069. goto bail;
  1070. }
  1071. }
  1072. } else {
  1073. BUG_ON(new_dentry->d_parent->d_inode != new_dir);
  1074. status = ocfs2_check_dir_for_entry(new_dir,
  1075. new_dentry->d_name.name,
  1076. new_dentry->d_name.len);
  1077. if (status)
  1078. goto bail;
  1079. status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,
  1080. new_dentry->d_name.name,
  1081. new_dentry->d_name.len,
  1082. &target_insert);
  1083. if (status < 0) {
  1084. mlog_errno(status);
  1085. goto bail;
  1086. }
  1087. }
  1088. handle = ocfs2_start_trans(osb, ocfs2_rename_credits(osb->sb));
  1089. if (IS_ERR(handle)) {
  1090. status = PTR_ERR(handle);
  1091. handle = NULL;
  1092. mlog_errno(status);
  1093. goto bail;
  1094. }
  1095. if (target_exists) {
  1096. if (S_ISDIR(new_inode->i_mode)) {
  1097. if (!ocfs2_empty_dir(new_inode) ||
  1098. new_inode->i_nlink != 2) {
  1099. status = -ENOTEMPTY;
  1100. goto bail;
  1101. }
  1102. }
  1103. status = ocfs2_journal_access_di(handle, new_inode, newfe_bh,
  1104. OCFS2_JOURNAL_ACCESS_WRITE);
  1105. if (status < 0) {
  1106. mlog_errno(status);
  1107. goto bail;
  1108. }
  1109. if (S_ISDIR(new_inode->i_mode) ||
  1110. (newfe->i_links_count == cpu_to_le16(1))){
  1111. status = ocfs2_orphan_add(osb, handle, new_inode,
  1112. newfe, orphan_name,
  1113. &orphan_insert, orphan_dir);
  1114. if (status < 0) {
  1115. mlog_errno(status);
  1116. goto bail;
  1117. }
  1118. }
  1119. /* change the dirent to point to the correct inode */
  1120. status = ocfs2_update_entry(new_dir, handle, &target_lookup_res,
  1121. old_inode);
  1122. if (status < 0) {
  1123. mlog_errno(status);
  1124. goto bail;
  1125. }
  1126. new_dir->i_version++;
  1127. if (S_ISDIR(new_inode->i_mode))
  1128. newfe->i_links_count = 0;
  1129. else
  1130. le16_add_cpu(&newfe->i_links_count, -1);
  1131. status = ocfs2_journal_dirty(handle, newfe_bh);
  1132. if (status < 0) {
  1133. mlog_errno(status);
  1134. goto bail;
  1135. }
  1136. } else {
  1137. /* if the name was not found in new_dir, add it now */
  1138. status = ocfs2_add_entry(handle, new_dentry, old_inode,
  1139. OCFS2_I(old_inode)->ip_blkno,
  1140. new_dir_bh, &target_insert);
  1141. }
  1142. old_inode->i_ctime = CURRENT_TIME;
  1143. mark_inode_dirty(old_inode);
  1144. status = ocfs2_journal_access_di(handle, old_inode, old_inode_bh,
  1145. OCFS2_JOURNAL_ACCESS_WRITE);
  1146. if (status >= 0) {
  1147. old_di = (struct ocfs2_dinode *) old_inode_bh->b_data;
  1148. old_di->i_ctime = cpu_to_le64(old_inode->i_ctime.tv_sec);
  1149. old_di->i_ctime_nsec = cpu_to_le32(old_inode->i_ctime.tv_nsec);
  1150. status = ocfs2_journal_dirty(handle, old_inode_bh);
  1151. if (status < 0)
  1152. mlog_errno(status);
  1153. } else
  1154. mlog_errno(status);
  1155. /*
  1156. * Now that the name has been added to new_dir, remove the old name.
  1157. *
  1158. * We don't keep any directory entry context around until now
  1159. * because the insert might have changed the type of directory
  1160. * we're dealing with.
  1161. */
  1162. status = ocfs2_find_entry(old_dentry->d_name.name,
  1163. old_dentry->d_name.len, old_dir,
  1164. &old_entry_lookup);
  1165. if (status)
  1166. goto bail;
  1167. status = ocfs2_delete_entry(handle, old_dir, &old_entry_lookup);
  1168. if (status < 0) {
  1169. mlog_errno(status);
  1170. goto bail;
  1171. }
  1172. if (new_inode) {
  1173. new_inode->i_nlink--;
  1174. new_inode->i_ctime = CURRENT_TIME;
  1175. }
  1176. old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
  1177. if (update_dot_dot) {
  1178. status = ocfs2_update_entry(old_inode, handle,
  1179. &old_inode_dot_dot_res, new_dir);
  1180. old_dir->i_nlink--;
  1181. if (new_inode) {
  1182. new_inode->i_nlink--;
  1183. } else {
  1184. inc_nlink(new_dir);
  1185. mark_inode_dirty(new_dir);
  1186. }
  1187. }
  1188. mark_inode_dirty(old_dir);
  1189. ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh);
  1190. if (new_inode) {
  1191. mark_inode_dirty(new_inode);
  1192. ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh);
  1193. }
  1194. if (old_dir != new_dir) {
  1195. /* Keep the same times on both directories.*/
  1196. new_dir->i_ctime = new_dir->i_mtime = old_dir->i_ctime;
  1197. /*
  1198. * This will also pick up the i_nlink change from the
  1199. * block above.
  1200. */
  1201. ocfs2_mark_inode_dirty(handle, new_dir, new_dir_bh);
  1202. }
  1203. if (old_dir_nlink != old_dir->i_nlink) {
  1204. if (!old_dir_bh) {
  1205. mlog(ML_ERROR, "need to change nlink for old dir "
  1206. "%llu from %d to %d but bh is NULL!\n",
  1207. (unsigned long long)OCFS2_I(old_dir)->ip_blkno,
  1208. (int)old_dir_nlink, old_dir->i_nlink);
  1209. } else {
  1210. struct ocfs2_dinode *fe;
  1211. status = ocfs2_journal_access_di(handle, old_dir,
  1212. old_dir_bh,
  1213. OCFS2_JOURNAL_ACCESS_WRITE);
  1214. fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
  1215. fe->i_links_count = cpu_to_le16(old_dir->i_nlink);
  1216. status = ocfs2_journal_dirty(handle, old_dir_bh);
  1217. }
  1218. }
  1219. ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
  1220. status = 0;
  1221. bail:
  1222. if (rename_lock)
  1223. ocfs2_rename_unlock(osb);
  1224. if (handle)
  1225. ocfs2_commit_trans(osb, handle);
  1226. if (parents_locked)
  1227. ocfs2_double_unlock(old_dir, new_dir);
  1228. if (old_child_locked)
  1229. ocfs2_inode_unlock(old_inode, 1);
  1230. if (new_child_locked)
  1231. ocfs2_inode_unlock(new_inode, 1);
  1232. if (orphan_dir) {
  1233. /* This was locked for us in ocfs2_prepare_orphan_dir() */
  1234. ocfs2_inode_unlock(orphan_dir, 1);
  1235. mutex_unlock(&orphan_dir->i_mutex);
  1236. iput(orphan_dir);
  1237. }
  1238. if (new_inode)
  1239. sync_mapping_buffers(old_inode->i_mapping);
  1240. if (new_inode)
  1241. iput(new_inode);
  1242. ocfs2_free_dir_lookup_result(&target_lookup_res);
  1243. ocfs2_free_dir_lookup_result(&old_entry_lookup);
  1244. ocfs2_free_dir_lookup_result(&old_inode_dot_dot_res);
  1245. ocfs2_free_dir_lookup_result(&orphan_insert);
  1246. ocfs2_free_dir_lookup_result(&target_insert);
  1247. brelse(newfe_bh);
  1248. brelse(old_inode_bh);
  1249. brelse(old_dir_bh);
  1250. brelse(new_dir_bh);
  1251. brelse(orphan_entry_bh);
  1252. brelse(insert_entry_bh);
  1253. mlog_exit(status);
  1254. return status;
  1255. }
  1256. /*
  1257. * we expect i_size = strlen(symname). Copy symname into the file
  1258. * data, including the null terminator.
  1259. */
  1260. static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
  1261. handle_t *handle,
  1262. struct inode *inode,
  1263. const char *symname)
  1264. {
  1265. struct buffer_head **bhs = NULL;
  1266. const char *c;
  1267. struct super_block *sb = osb->sb;
  1268. u64 p_blkno, p_blocks;
  1269. int virtual, blocks, status, i, bytes_left;
  1270. bytes_left = i_size_read(inode) + 1;
  1271. /* we can't trust i_blocks because we're actually going to
  1272. * write i_size + 1 bytes. */
  1273. blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
  1274. mlog_entry("i_blocks = %llu, i_size = %llu, blocks = %d\n",
  1275. (unsigned long long)inode->i_blocks,
  1276. i_size_read(inode), blocks);
  1277. /* Sanity check -- make sure we're going to fit. */
  1278. if (bytes_left >
  1279. ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
  1280. status = -EIO;
  1281. mlog_errno(status);
  1282. goto bail;
  1283. }
  1284. bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
  1285. if (!bhs) {
  1286. status = -ENOMEM;
  1287. mlog_errno(status);
  1288. goto bail;
  1289. }
  1290. status = ocfs2_extent_map_get_blocks(inode, 0, &p_blkno, &p_blocks,
  1291. NULL);
  1292. if (status < 0) {
  1293. mlog_errno(status);
  1294. goto bail;
  1295. }
  1296. /* links can never be larger than one cluster so we know this
  1297. * is all going to be contiguous, but do a sanity check
  1298. * anyway. */
  1299. if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
  1300. status = -EIO;
  1301. mlog_errno(status);
  1302. goto bail;
  1303. }
  1304. virtual = 0;
  1305. while(bytes_left > 0) {
  1306. c = &symname[virtual * sb->s_blocksize];
  1307. bhs[virtual] = sb_getblk(sb, p_blkno);
  1308. if (!bhs[virtual]) {
  1309. status = -ENOMEM;
  1310. mlog_errno(status);
  1311. goto bail;
  1312. }
  1313. ocfs2_set_new_buffer_uptodate(inode, bhs[virtual]);
  1314. status = ocfs2_journal_access(handle, inode, bhs[virtual],
  1315. OCFS2_JOURNAL_ACCESS_CREATE);
  1316. if (status < 0) {
  1317. mlog_errno(status);
  1318. goto bail;
  1319. }
  1320. memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
  1321. memcpy(bhs[virtual]->b_data, c,
  1322. (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
  1323. bytes_left);
  1324. status = ocfs2_journal_dirty(handle, bhs[virtual]);
  1325. if (status < 0) {
  1326. mlog_errno(status);
  1327. goto bail;
  1328. }
  1329. virtual++;
  1330. p_blkno++;
  1331. bytes_left -= sb->s_blocksize;
  1332. }
  1333. status = 0;
  1334. bail:
  1335. if (bhs) {
  1336. for(i = 0; i < blocks; i++)
  1337. brelse(bhs[i]);
  1338. kfree(bhs);
  1339. }
  1340. mlog_exit(status);
  1341. return status;
  1342. }
  1343. static int ocfs2_symlink(struct inode *dir,
  1344. struct dentry *dentry,
  1345. const char *symname)
  1346. {
  1347. int status, l, credits;
  1348. u64 newsize;
  1349. struct ocfs2_super *osb = NULL;
  1350. struct inode *inode = NULL;
  1351. struct super_block *sb;
  1352. struct buffer_head *new_fe_bh = NULL;
  1353. struct buffer_head *parent_fe_bh = NULL;
  1354. struct ocfs2_dinode *fe = NULL;
  1355. struct ocfs2_dinode *dirfe;
  1356. handle_t *handle = NULL;
  1357. struct ocfs2_alloc_context *inode_ac = NULL;
  1358. struct ocfs2_alloc_context *data_ac = NULL;
  1359. struct ocfs2_alloc_context *xattr_ac = NULL;
  1360. int want_clusters = 0;
  1361. int xattr_credits = 0;
  1362. struct ocfs2_security_xattr_info si = {
  1363. .enable = 1,
  1364. };
  1365. int did_quota = 0, did_quota_inode = 0;
  1366. struct ocfs2_dir_lookup_result lookup = { NULL, };
  1367. mlog_entry("(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir,
  1368. dentry, symname, dentry->d_name.len, dentry->d_name.name);
  1369. sb = dir->i_sb;
  1370. osb = OCFS2_SB(sb);
  1371. l = strlen(symname) + 1;
  1372. credits = ocfs2_calc_symlink_credits(sb);
  1373. /* lock the parent directory */
  1374. status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
  1375. if (status < 0) {
  1376. if (status != -ENOENT)
  1377. mlog_errno(status);
  1378. return status;
  1379. }
  1380. dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
  1381. if (!dirfe->i_links_count) {
  1382. /* can't make a file in a deleted directory. */
  1383. status = -ENOENT;
  1384. goto bail;
  1385. }
  1386. status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
  1387. dentry->d_name.len);
  1388. if (status)
  1389. goto bail;
  1390. status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
  1391. dentry->d_name.name,
  1392. dentry->d_name.len, &lookup);
  1393. if (status < 0) {
  1394. mlog_errno(status);
  1395. goto bail;
  1396. }
  1397. status = ocfs2_reserve_new_inode(osb, &inode_ac);
  1398. if (status < 0) {
  1399. if (status != -ENOSPC)
  1400. mlog_errno(status);
  1401. goto bail;
  1402. }
  1403. inode = ocfs2_get_init_inode(dir, S_IFLNK | S_IRWXUGO);
  1404. if (!inode) {
  1405. status = -ENOMEM;
  1406. mlog_errno(status);
  1407. goto bail;
  1408. }
  1409. /* get security xattr */
  1410. status = ocfs2_init_security_get(inode, dir, &si);
  1411. if (status) {
  1412. if (status == -EOPNOTSUPP)
  1413. si.enable = 0;
  1414. else {
  1415. mlog_errno(status);
  1416. goto bail;
  1417. }
  1418. }
  1419. /* calculate meta data/clusters for setting security xattr */
  1420. if (si.enable) {
  1421. status = ocfs2_calc_security_init(dir, &si, &want_clusters,
  1422. &xattr_credits, &xattr_ac);
  1423. if (status < 0) {
  1424. mlog_errno(status);
  1425. goto bail;
  1426. }
  1427. }
  1428. /* don't reserve bitmap space for fast symlinks. */
  1429. if (l > ocfs2_fast_symlink_chars(sb))
  1430. want_clusters += 1;
  1431. status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac);
  1432. if (status < 0) {
  1433. if (status != -ENOSPC)
  1434. mlog_errno(status);
  1435. goto bail;
  1436. }
  1437. handle = ocfs2_start_trans(osb, credits + xattr_credits);
  1438. if (IS_ERR(handle)) {
  1439. status = PTR_ERR(handle);
  1440. handle = NULL;
  1441. mlog_errno(status);
  1442. goto bail;
  1443. }
  1444. /* We don't use standard VFS wrapper because we don't want vfs_dq_init
  1445. * to be called. */
  1446. if (sb_any_quota_active(osb->sb) &&
  1447. osb->sb->dq_op->alloc_inode(inode, 1) == NO_QUOTA) {
  1448. status = -EDQUOT;
  1449. goto bail;
  1450. }
  1451. did_quota_inode = 1;
  1452. status = ocfs2_mknod_locked(osb, dir, inode, dentry,
  1453. 0, &new_fe_bh, parent_fe_bh, handle,
  1454. inode_ac);
  1455. if (status < 0) {
  1456. mlog_errno(status);
  1457. goto bail;
  1458. }
  1459. fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
  1460. inode->i_rdev = 0;
  1461. newsize = l - 1;
  1462. if (l > ocfs2_fast_symlink_chars(sb)) {
  1463. u32 offset = 0;
  1464. inode->i_op = &ocfs2_symlink_inode_operations;
  1465. if (vfs_dq_alloc_space_nodirty(inode,
  1466. ocfs2_clusters_to_bytes(osb->sb, 1))) {
  1467. status = -EDQUOT;
  1468. goto bail;
  1469. }
  1470. did_quota = 1;
  1471. status = ocfs2_add_inode_data(osb, inode, &offset, 1, 0,
  1472. new_fe_bh,
  1473. handle, data_ac, NULL,
  1474. NULL);
  1475. if (status < 0) {
  1476. if (status != -ENOSPC && status != -EINTR) {
  1477. mlog(ML_ERROR,
  1478. "Failed to extend file to %llu\n",
  1479. (unsigned long long)newsize);
  1480. mlog_errno(status);
  1481. status = -ENOSPC;
  1482. }
  1483. goto bail;
  1484. }
  1485. i_size_write(inode, newsize);
  1486. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1487. } else {
  1488. inode->i_op = &ocfs2_fast_symlink_inode_operations;
  1489. memcpy((char *) fe->id2.i_symlink, symname, l);
  1490. i_size_write(inode, newsize);
  1491. inode->i_blocks = 0;
  1492. }
  1493. status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
  1494. if (status < 0) {
  1495. mlog_errno(status);
  1496. goto bail;
  1497. }
  1498. if (!ocfs2_inode_is_fast_symlink(inode)) {
  1499. status = ocfs2_create_symlink_data(osb, handle, inode,
  1500. symname);
  1501. if (status < 0) {
  1502. mlog_errno(status);
  1503. goto bail;
  1504. }
  1505. }
  1506. if (si.enable) {
  1507. status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si,
  1508. xattr_ac, data_ac);
  1509. if (status < 0) {
  1510. mlog_errno(status);
  1511. goto bail;
  1512. }
  1513. }
  1514. status = ocfs2_add_entry(handle, dentry, inode,
  1515. le64_to_cpu(fe->i_blkno), parent_fe_bh,
  1516. &lookup);
  1517. if (status < 0) {
  1518. mlog_errno(status);
  1519. goto bail;
  1520. }
  1521. status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
  1522. if (status) {
  1523. mlog_errno(status);
  1524. goto bail;
  1525. }
  1526. insert_inode_hash(inode);
  1527. dentry->d_op = &ocfs2_dentry_ops;
  1528. d_instantiate(dentry, inode);
  1529. bail:
  1530. if (status < 0 && did_quota)
  1531. vfs_dq_free_space_nodirty(inode,
  1532. ocfs2_clusters_to_bytes(osb->sb, 1));
  1533. if (status < 0 && did_quota_inode)
  1534. vfs_dq_free_inode(inode);
  1535. if (handle)
  1536. ocfs2_commit_trans(osb, handle);
  1537. ocfs2_inode_unlock(dir, 1);
  1538. brelse(new_fe_bh);
  1539. brelse(parent_fe_bh);
  1540. kfree(si.name);
  1541. kfree(si.value);
  1542. ocfs2_free_dir_lookup_result(&lookup);
  1543. if (inode_ac)
  1544. ocfs2_free_alloc_context(inode_ac);
  1545. if (data_ac)
  1546. ocfs2_free_alloc_context(data_ac);
  1547. if (xattr_ac)
  1548. ocfs2_free_alloc_context(xattr_ac);
  1549. if ((status < 0) && inode) {
  1550. clear_nlink(inode);
  1551. iput(inode);
  1552. }
  1553. mlog_exit(status);
  1554. return status;
  1555. }
  1556. static int ocfs2_blkno_stringify(u64 blkno, char *name)
  1557. {
  1558. int status, namelen;
  1559. mlog_entry_void();
  1560. namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
  1561. (long long)blkno);
  1562. if (namelen <= 0) {
  1563. if (namelen)
  1564. status = namelen;
  1565. else
  1566. status = -EINVAL;
  1567. mlog_errno(status);
  1568. goto bail;
  1569. }
  1570. if (namelen != OCFS2_ORPHAN_NAMELEN) {
  1571. status = -EINVAL;
  1572. mlog_errno(status);
  1573. goto bail;
  1574. }
  1575. mlog(0, "built filename '%s' for orphan dir (len=%d)\n", name,
  1576. namelen);
  1577. status = 0;
  1578. bail:
  1579. mlog_exit(status);
  1580. return status;
  1581. }
  1582. static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
  1583. struct inode **ret_orphan_dir,
  1584. struct inode *inode,
  1585. char *name,
  1586. struct ocfs2_dir_lookup_result *lookup)
  1587. {
  1588. struct inode *orphan_dir_inode;
  1589. struct buffer_head *orphan_dir_bh = NULL;
  1590. int status = 0;
  1591. status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
  1592. if (status < 0) {
  1593. mlog_errno(status);
  1594. return status;
  1595. }
  1596. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  1597. ORPHAN_DIR_SYSTEM_INODE,
  1598. osb->slot_num);
  1599. if (!orphan_dir_inode) {
  1600. status = -ENOENT;
  1601. mlog_errno(status);
  1602. return status;
  1603. }
  1604. mutex_lock(&orphan_dir_inode->i_mutex);
  1605. status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
  1606. if (status < 0) {
  1607. mlog_errno(status);
  1608. goto leave;
  1609. }
  1610. status = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
  1611. orphan_dir_bh, name,
  1612. OCFS2_ORPHAN_NAMELEN, lookup);
  1613. if (status < 0) {
  1614. ocfs2_inode_unlock(orphan_dir_inode, 1);
  1615. mlog_errno(status);
  1616. goto leave;
  1617. }
  1618. *ret_orphan_dir = orphan_dir_inode;
  1619. leave:
  1620. if (status) {
  1621. mutex_unlock(&orphan_dir_inode->i_mutex);
  1622. iput(orphan_dir_inode);
  1623. }
  1624. brelse(orphan_dir_bh);
  1625. mlog_exit(status);
  1626. return status;
  1627. }
  1628. static int ocfs2_orphan_add(struct ocfs2_super *osb,
  1629. handle_t *handle,
  1630. struct inode *inode,
  1631. struct ocfs2_dinode *fe,
  1632. char *name,
  1633. struct ocfs2_dir_lookup_result *lookup,
  1634. struct inode *orphan_dir_inode)
  1635. {
  1636. struct buffer_head *orphan_dir_bh = NULL;
  1637. int status = 0;
  1638. struct ocfs2_dinode *orphan_fe;
  1639. mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
  1640. status = ocfs2_read_inode_block(orphan_dir_inode, &orphan_dir_bh);
  1641. if (status < 0) {
  1642. mlog_errno(status);
  1643. goto leave;
  1644. }
  1645. status = ocfs2_journal_access_di(handle, orphan_dir_inode, orphan_dir_bh,
  1646. OCFS2_JOURNAL_ACCESS_WRITE);
  1647. if (status < 0) {
  1648. mlog_errno(status);
  1649. goto leave;
  1650. }
  1651. /* we're a cluster, and nlink can change on disk from
  1652. * underneath us... */
  1653. orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
  1654. if (S_ISDIR(inode->i_mode))
  1655. le16_add_cpu(&orphan_fe->i_links_count, 1);
  1656. orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
  1657. status = ocfs2_journal_dirty(handle, orphan_dir_bh);
  1658. if (status < 0) {
  1659. mlog_errno(status);
  1660. goto leave;
  1661. }
  1662. status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
  1663. OCFS2_ORPHAN_NAMELEN, inode,
  1664. OCFS2_I(inode)->ip_blkno,
  1665. orphan_dir_bh, lookup);
  1666. if (status < 0) {
  1667. mlog_errno(status);
  1668. goto leave;
  1669. }
  1670. le32_add_cpu(&fe->i_flags, OCFS2_ORPHANED_FL);
  1671. /* Record which orphan dir our inode now resides
  1672. * in. delete_inode will use this to determine which orphan
  1673. * dir to lock. */
  1674. fe->i_orphaned_slot = cpu_to_le16(osb->slot_num);
  1675. mlog(0, "Inode %llu orphaned in slot %d\n",
  1676. (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num);
  1677. leave:
  1678. brelse(orphan_dir_bh);
  1679. mlog_exit(status);
  1680. return status;
  1681. }
  1682. /* unlike orphan_add, we expect the orphan dir to already be locked here. */
  1683. int ocfs2_orphan_del(struct ocfs2_super *osb,
  1684. handle_t *handle,
  1685. struct inode *orphan_dir_inode,
  1686. struct inode *inode,
  1687. struct buffer_head *orphan_dir_bh)
  1688. {
  1689. char name[OCFS2_ORPHAN_NAMELEN + 1];
  1690. struct ocfs2_dinode *orphan_fe;
  1691. int status = 0;
  1692. struct ocfs2_dir_lookup_result lookup = { NULL, };
  1693. mlog_entry_void();
  1694. status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
  1695. if (status < 0) {
  1696. mlog_errno(status);
  1697. goto leave;
  1698. }
  1699. mlog(0, "removing '%s' from orphan dir %llu (namelen=%d)\n",
  1700. name, (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
  1701. OCFS2_ORPHAN_NAMELEN);
  1702. /* find it's spot in the orphan directory */
  1703. status = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN, orphan_dir_inode,
  1704. &lookup);
  1705. if (status) {
  1706. mlog_errno(status);
  1707. goto leave;
  1708. }
  1709. /* remove it from the orphan directory */
  1710. status = ocfs2_delete_entry(handle, orphan_dir_inode, &lookup);
  1711. if (status < 0) {
  1712. mlog_errno(status);
  1713. goto leave;
  1714. }
  1715. status = ocfs2_journal_access_di(handle,orphan_dir_inode, orphan_dir_bh,
  1716. OCFS2_JOURNAL_ACCESS_WRITE);
  1717. if (status < 0) {
  1718. mlog_errno(status);
  1719. goto leave;
  1720. }
  1721. /* do the i_nlink dance! :) */
  1722. orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
  1723. if (S_ISDIR(inode->i_mode))
  1724. le16_add_cpu(&orphan_fe->i_links_count, -1);
  1725. orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
  1726. status = ocfs2_journal_dirty(handle, orphan_dir_bh);
  1727. if (status < 0) {
  1728. mlog_errno(status);
  1729. goto leave;
  1730. }
  1731. leave:
  1732. ocfs2_free_dir_lookup_result(&lookup);
  1733. mlog_exit(status);
  1734. return status;
  1735. }
  1736. const struct inode_operations ocfs2_dir_iops = {
  1737. .create = ocfs2_create,
  1738. .lookup = ocfs2_lookup,
  1739. .link = ocfs2_link,
  1740. .unlink = ocfs2_unlink,
  1741. .rmdir = ocfs2_unlink,
  1742. .symlink = ocfs2_symlink,
  1743. .mkdir = ocfs2_mkdir,
  1744. .mknod = ocfs2_mknod,
  1745. .rename = ocfs2_rename,
  1746. .setattr = ocfs2_setattr,
  1747. .getattr = ocfs2_getattr,
  1748. .permission = ocfs2_permission,
  1749. .setxattr = generic_setxattr,
  1750. .getxattr = generic_getxattr,
  1751. .listxattr = ocfs2_listxattr,
  1752. .removexattr = generic_removexattr,
  1753. };