namei.c 55 KB

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