namei.c 55 KB

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