namei.c 56 KB

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