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