namei.c 54 KB

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