namei.c 55 KB

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