namei.c 55 KB

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