namei.c 56 KB

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