namei.c 48 KB

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