namei.c 55 KB

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