namei.c 55 KB

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