file.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * file.c
  5. *
  6. * File open, close, extend, truncate
  7. *
  8. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. * Boston, MA 021110-1307, USA.
  24. */
  25. #include <linux/capability.h>
  26. #include <linux/fs.h>
  27. #include <linux/types.h>
  28. #include <linux/slab.h>
  29. #include <linux/highmem.h>
  30. #include <linux/pagemap.h>
  31. #include <linux/uio.h>
  32. #include <linux/sched.h>
  33. #include <linux/splice.h>
  34. #include <linux/mount.h>
  35. #include <linux/writeback.h>
  36. #include <linux/falloc.h>
  37. #define MLOG_MASK_PREFIX ML_INODE
  38. #include <cluster/masklog.h>
  39. #include "ocfs2.h"
  40. #include "alloc.h"
  41. #include "aops.h"
  42. #include "dir.h"
  43. #include "dlmglue.h"
  44. #include "extent_map.h"
  45. #include "file.h"
  46. #include "sysfile.h"
  47. #include "inode.h"
  48. #include "ioctl.h"
  49. #include "journal.h"
  50. #include "locks.h"
  51. #include "mmap.h"
  52. #include "suballoc.h"
  53. #include "super.h"
  54. #include "buffer_head_io.h"
  55. static int ocfs2_sync_inode(struct inode *inode)
  56. {
  57. filemap_fdatawrite(inode->i_mapping);
  58. return sync_mapping_buffers(inode->i_mapping);
  59. }
  60. static int ocfs2_init_file_private(struct inode *inode, struct file *file)
  61. {
  62. struct ocfs2_file_private *fp;
  63. fp = kzalloc(sizeof(struct ocfs2_file_private), GFP_KERNEL);
  64. if (!fp)
  65. return -ENOMEM;
  66. fp->fp_file = file;
  67. mutex_init(&fp->fp_mutex);
  68. ocfs2_file_lock_res_init(&fp->fp_flock, fp);
  69. file->private_data = fp;
  70. return 0;
  71. }
  72. static void ocfs2_free_file_private(struct inode *inode, struct file *file)
  73. {
  74. struct ocfs2_file_private *fp = file->private_data;
  75. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  76. if (fp) {
  77. ocfs2_simple_drop_lockres(osb, &fp->fp_flock);
  78. ocfs2_lock_res_free(&fp->fp_flock);
  79. kfree(fp);
  80. file->private_data = NULL;
  81. }
  82. }
  83. static int ocfs2_file_open(struct inode *inode, struct file *file)
  84. {
  85. int status;
  86. int mode = file->f_flags;
  87. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  88. mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
  89. file->f_path.dentry->d_name.len, file->f_path.dentry->d_name.name);
  90. spin_lock(&oi->ip_lock);
  91. /* Check that the inode hasn't been wiped from disk by another
  92. * node. If it hasn't then we're safe as long as we hold the
  93. * spin lock until our increment of open count. */
  94. if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
  95. spin_unlock(&oi->ip_lock);
  96. status = -ENOENT;
  97. goto leave;
  98. }
  99. if (mode & O_DIRECT)
  100. oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT;
  101. oi->ip_open_count++;
  102. spin_unlock(&oi->ip_lock);
  103. status = ocfs2_init_file_private(inode, file);
  104. if (status) {
  105. /*
  106. * We want to set open count back if we're failing the
  107. * open.
  108. */
  109. spin_lock(&oi->ip_lock);
  110. oi->ip_open_count--;
  111. spin_unlock(&oi->ip_lock);
  112. }
  113. leave:
  114. mlog_exit(status);
  115. return status;
  116. }
  117. static int ocfs2_file_release(struct inode *inode, struct file *file)
  118. {
  119. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  120. mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
  121. file->f_path.dentry->d_name.len,
  122. file->f_path.dentry->d_name.name);
  123. spin_lock(&oi->ip_lock);
  124. if (!--oi->ip_open_count)
  125. oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT;
  126. spin_unlock(&oi->ip_lock);
  127. ocfs2_free_file_private(inode, file);
  128. mlog_exit(0);
  129. return 0;
  130. }
  131. static int ocfs2_dir_open(struct inode *inode, struct file *file)
  132. {
  133. return ocfs2_init_file_private(inode, file);
  134. }
  135. static int ocfs2_dir_release(struct inode *inode, struct file *file)
  136. {
  137. ocfs2_free_file_private(inode, file);
  138. return 0;
  139. }
  140. static int ocfs2_sync_file(struct file *file,
  141. struct dentry *dentry,
  142. int datasync)
  143. {
  144. int err = 0;
  145. journal_t *journal;
  146. struct inode *inode = dentry->d_inode;
  147. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  148. mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", file, dentry, datasync,
  149. dentry->d_name.len, dentry->d_name.name);
  150. err = ocfs2_sync_inode(dentry->d_inode);
  151. if (err)
  152. goto bail;
  153. journal = osb->journal->j_journal;
  154. err = journal_force_commit(journal);
  155. bail:
  156. mlog_exit(err);
  157. return (err < 0) ? -EIO : 0;
  158. }
  159. int ocfs2_should_update_atime(struct inode *inode,
  160. struct vfsmount *vfsmnt)
  161. {
  162. struct timespec now;
  163. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  164. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  165. return 0;
  166. if ((inode->i_flags & S_NOATIME) ||
  167. ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode)))
  168. return 0;
  169. /*
  170. * We can be called with no vfsmnt structure - NFSD will
  171. * sometimes do this.
  172. *
  173. * Note that our action here is different than touch_atime() -
  174. * if we can't tell whether this is a noatime mount, then we
  175. * don't know whether to trust the value of s_atime_quantum.
  176. */
  177. if (vfsmnt == NULL)
  178. return 0;
  179. if ((vfsmnt->mnt_flags & MNT_NOATIME) ||
  180. ((vfsmnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
  181. return 0;
  182. if (vfsmnt->mnt_flags & MNT_RELATIME) {
  183. if ((timespec_compare(&inode->i_atime, &inode->i_mtime) <= 0) ||
  184. (timespec_compare(&inode->i_atime, &inode->i_ctime) <= 0))
  185. return 1;
  186. return 0;
  187. }
  188. now = CURRENT_TIME;
  189. if ((now.tv_sec - inode->i_atime.tv_sec <= osb->s_atime_quantum))
  190. return 0;
  191. else
  192. return 1;
  193. }
  194. int ocfs2_update_inode_atime(struct inode *inode,
  195. struct buffer_head *bh)
  196. {
  197. int ret;
  198. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  199. handle_t *handle;
  200. struct ocfs2_dinode *di = (struct ocfs2_dinode *) bh->b_data;
  201. mlog_entry_void();
  202. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  203. if (handle == NULL) {
  204. ret = -ENOMEM;
  205. mlog_errno(ret);
  206. goto out;
  207. }
  208. ret = ocfs2_journal_access(handle, inode, bh,
  209. OCFS2_JOURNAL_ACCESS_WRITE);
  210. if (ret) {
  211. mlog_errno(ret);
  212. goto out_commit;
  213. }
  214. /*
  215. * Don't use ocfs2_mark_inode_dirty() here as we don't always
  216. * have i_mutex to guard against concurrent changes to other
  217. * inode fields.
  218. */
  219. inode->i_atime = CURRENT_TIME;
  220. di->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
  221. di->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
  222. ret = ocfs2_journal_dirty(handle, bh);
  223. if (ret < 0)
  224. mlog_errno(ret);
  225. out_commit:
  226. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  227. out:
  228. mlog_exit(ret);
  229. return ret;
  230. }
  231. static int ocfs2_set_inode_size(handle_t *handle,
  232. struct inode *inode,
  233. struct buffer_head *fe_bh,
  234. u64 new_i_size)
  235. {
  236. int status;
  237. mlog_entry_void();
  238. i_size_write(inode, new_i_size);
  239. inode->i_blocks = ocfs2_inode_sector_count(inode);
  240. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  241. status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  242. if (status < 0) {
  243. mlog_errno(status);
  244. goto bail;
  245. }
  246. bail:
  247. mlog_exit(status);
  248. return status;
  249. }
  250. static int ocfs2_simple_size_update(struct inode *inode,
  251. struct buffer_head *di_bh,
  252. u64 new_i_size)
  253. {
  254. int ret;
  255. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  256. handle_t *handle = NULL;
  257. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  258. if (handle == NULL) {
  259. ret = -ENOMEM;
  260. mlog_errno(ret);
  261. goto out;
  262. }
  263. ret = ocfs2_set_inode_size(handle, inode, di_bh,
  264. new_i_size);
  265. if (ret < 0)
  266. mlog_errno(ret);
  267. ocfs2_commit_trans(osb, handle);
  268. out:
  269. return ret;
  270. }
  271. static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
  272. struct inode *inode,
  273. struct buffer_head *fe_bh,
  274. u64 new_i_size)
  275. {
  276. int status;
  277. handle_t *handle;
  278. struct ocfs2_dinode *di;
  279. u64 cluster_bytes;
  280. mlog_entry_void();
  281. /* TODO: This needs to actually orphan the inode in this
  282. * transaction. */
  283. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  284. if (IS_ERR(handle)) {
  285. status = PTR_ERR(handle);
  286. mlog_errno(status);
  287. goto out;
  288. }
  289. status = ocfs2_journal_access(handle, inode, fe_bh,
  290. OCFS2_JOURNAL_ACCESS_WRITE);
  291. if (status < 0) {
  292. mlog_errno(status);
  293. goto out_commit;
  294. }
  295. /*
  296. * Do this before setting i_size.
  297. */
  298. cluster_bytes = ocfs2_align_bytes_to_clusters(inode->i_sb, new_i_size);
  299. status = ocfs2_zero_range_for_truncate(inode, handle, new_i_size,
  300. cluster_bytes);
  301. if (status) {
  302. mlog_errno(status);
  303. goto out_commit;
  304. }
  305. i_size_write(inode, new_i_size);
  306. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  307. di = (struct ocfs2_dinode *) fe_bh->b_data;
  308. di->i_size = cpu_to_le64(new_i_size);
  309. di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
  310. di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  311. status = ocfs2_journal_dirty(handle, fe_bh);
  312. if (status < 0)
  313. mlog_errno(status);
  314. out_commit:
  315. ocfs2_commit_trans(osb, handle);
  316. out:
  317. mlog_exit(status);
  318. return status;
  319. }
  320. static int ocfs2_truncate_file(struct inode *inode,
  321. struct buffer_head *di_bh,
  322. u64 new_i_size)
  323. {
  324. int status = 0;
  325. struct ocfs2_dinode *fe = NULL;
  326. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  327. struct ocfs2_truncate_context *tc = NULL;
  328. mlog_entry("(inode = %llu, new_i_size = %llu\n",
  329. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  330. (unsigned long long)new_i_size);
  331. fe = (struct ocfs2_dinode *) di_bh->b_data;
  332. if (!OCFS2_IS_VALID_DINODE(fe)) {
  333. OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
  334. status = -EIO;
  335. goto bail;
  336. }
  337. mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode),
  338. "Inode %llu, inode i_size = %lld != di "
  339. "i_size = %llu, i_flags = 0x%x\n",
  340. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  341. i_size_read(inode),
  342. (unsigned long long)le64_to_cpu(fe->i_size),
  343. le32_to_cpu(fe->i_flags));
  344. if (new_i_size > le64_to_cpu(fe->i_size)) {
  345. mlog(0, "asked to truncate file with size (%llu) to size (%llu)!\n",
  346. (unsigned long long)le64_to_cpu(fe->i_size),
  347. (unsigned long long)new_i_size);
  348. status = -EINVAL;
  349. mlog_errno(status);
  350. goto bail;
  351. }
  352. mlog(0, "inode %llu, i_size = %llu, new_i_size = %llu\n",
  353. (unsigned long long)le64_to_cpu(fe->i_blkno),
  354. (unsigned long long)le64_to_cpu(fe->i_size),
  355. (unsigned long long)new_i_size);
  356. /* lets handle the simple truncate cases before doing any more
  357. * cluster locking. */
  358. if (new_i_size == le64_to_cpu(fe->i_size))
  359. goto bail;
  360. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  361. /*
  362. * The inode lock forced other nodes to sync and drop their
  363. * pages, which (correctly) happens even if we have a truncate
  364. * without allocation change - ocfs2 cluster sizes can be much
  365. * greater than page size, so we have to truncate them
  366. * anyway.
  367. */
  368. unmap_mapping_range(inode->i_mapping, new_i_size + PAGE_SIZE - 1, 0, 1);
  369. truncate_inode_pages(inode->i_mapping, new_i_size);
  370. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  371. status = ocfs2_truncate_inline(inode, di_bh, new_i_size,
  372. i_size_read(inode), 1);
  373. if (status)
  374. mlog_errno(status);
  375. goto bail_unlock_sem;
  376. }
  377. /* alright, we're going to need to do a full blown alloc size
  378. * change. Orphan the inode so that recovery can complete the
  379. * truncate if necessary. This does the task of marking
  380. * i_size. */
  381. status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size);
  382. if (status < 0) {
  383. mlog_errno(status);
  384. goto bail_unlock_sem;
  385. }
  386. status = ocfs2_prepare_truncate(osb, inode, di_bh, &tc);
  387. if (status < 0) {
  388. mlog_errno(status);
  389. goto bail_unlock_sem;
  390. }
  391. status = ocfs2_commit_truncate(osb, inode, di_bh, tc);
  392. if (status < 0) {
  393. mlog_errno(status);
  394. goto bail_unlock_sem;
  395. }
  396. /* TODO: orphan dir cleanup here. */
  397. bail_unlock_sem:
  398. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  399. bail:
  400. mlog_exit(status);
  401. return status;
  402. }
  403. /*
  404. * extend file allocation only here.
  405. * we'll update all the disk stuff, and oip->alloc_size
  406. *
  407. * expect stuff to be locked, a transaction started and enough data /
  408. * metadata reservations in the contexts.
  409. *
  410. * Will return -EAGAIN, and a reason if a restart is needed.
  411. * If passed in, *reason will always be set, even in error.
  412. */
  413. int ocfs2_add_inode_data(struct ocfs2_super *osb,
  414. struct inode *inode,
  415. u32 *logical_offset,
  416. u32 clusters_to_add,
  417. int mark_unwritten,
  418. struct buffer_head *fe_bh,
  419. handle_t *handle,
  420. struct ocfs2_alloc_context *data_ac,
  421. struct ocfs2_alloc_context *meta_ac,
  422. enum ocfs2_alloc_restarted *reason_ret)
  423. {
  424. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
  425. struct ocfs2_extent_list *el = &fe->id2.i_list;
  426. return ocfs2_add_clusters_in_btree(osb, inode, logical_offset,
  427. clusters_to_add, mark_unwritten,
  428. fe_bh, el, handle,
  429. data_ac, meta_ac, reason_ret,
  430. OCFS2_DINODE_EXTENT, NULL);
  431. }
  432. static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start,
  433. u32 clusters_to_add, int mark_unwritten)
  434. {
  435. int status = 0;
  436. int restart_func = 0;
  437. int credits;
  438. u32 prev_clusters;
  439. struct buffer_head *bh = NULL;
  440. struct ocfs2_dinode *fe = NULL;
  441. handle_t *handle = NULL;
  442. struct ocfs2_alloc_context *data_ac = NULL;
  443. struct ocfs2_alloc_context *meta_ac = NULL;
  444. enum ocfs2_alloc_restarted why;
  445. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  446. mlog_entry("(clusters_to_add = %u)\n", clusters_to_add);
  447. /*
  448. * This function only exists for file systems which don't
  449. * support holes.
  450. */
  451. BUG_ON(mark_unwritten && !ocfs2_sparse_alloc(osb));
  452. status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
  453. OCFS2_BH_CACHED, inode);
  454. if (status < 0) {
  455. mlog_errno(status);
  456. goto leave;
  457. }
  458. fe = (struct ocfs2_dinode *) bh->b_data;
  459. if (!OCFS2_IS_VALID_DINODE(fe)) {
  460. OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
  461. status = -EIO;
  462. goto leave;
  463. }
  464. restart_all:
  465. BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
  466. mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u, "
  467. "clusters_to_add = %u\n",
  468. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  469. (long long)i_size_read(inode), le32_to_cpu(fe->i_clusters),
  470. clusters_to_add);
  471. status = ocfs2_lock_allocators(inode, bh, &fe->id2.i_list,
  472. clusters_to_add, 0, &data_ac,
  473. &meta_ac, OCFS2_DINODE_EXTENT, NULL);
  474. if (status) {
  475. mlog_errno(status);
  476. goto leave;
  477. }
  478. credits = ocfs2_calc_extend_credits(osb->sb, &fe->id2.i_list,
  479. clusters_to_add);
  480. handle = ocfs2_start_trans(osb, credits);
  481. if (IS_ERR(handle)) {
  482. status = PTR_ERR(handle);
  483. handle = NULL;
  484. mlog_errno(status);
  485. goto leave;
  486. }
  487. restarted_transaction:
  488. /* reserve a write to the file entry early on - that we if we
  489. * run out of credits in the allocation path, we can still
  490. * update i_size. */
  491. status = ocfs2_journal_access(handle, inode, bh,
  492. OCFS2_JOURNAL_ACCESS_WRITE);
  493. if (status < 0) {
  494. mlog_errno(status);
  495. goto leave;
  496. }
  497. prev_clusters = OCFS2_I(inode)->ip_clusters;
  498. status = ocfs2_add_inode_data(osb,
  499. inode,
  500. &logical_start,
  501. clusters_to_add,
  502. mark_unwritten,
  503. bh,
  504. handle,
  505. data_ac,
  506. meta_ac,
  507. &why);
  508. if ((status < 0) && (status != -EAGAIN)) {
  509. if (status != -ENOSPC)
  510. mlog_errno(status);
  511. goto leave;
  512. }
  513. status = ocfs2_journal_dirty(handle, bh);
  514. if (status < 0) {
  515. mlog_errno(status);
  516. goto leave;
  517. }
  518. spin_lock(&OCFS2_I(inode)->ip_lock);
  519. clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters);
  520. spin_unlock(&OCFS2_I(inode)->ip_lock);
  521. if (why != RESTART_NONE && clusters_to_add) {
  522. if (why == RESTART_META) {
  523. mlog(0, "restarting function.\n");
  524. restart_func = 1;
  525. } else {
  526. BUG_ON(why != RESTART_TRANS);
  527. mlog(0, "restarting transaction.\n");
  528. /* TODO: This can be more intelligent. */
  529. credits = ocfs2_calc_extend_credits(osb->sb,
  530. &fe->id2.i_list,
  531. clusters_to_add);
  532. status = ocfs2_extend_trans(handle, credits);
  533. if (status < 0) {
  534. /* handle still has to be committed at
  535. * this point. */
  536. status = -ENOMEM;
  537. mlog_errno(status);
  538. goto leave;
  539. }
  540. goto restarted_transaction;
  541. }
  542. }
  543. mlog(0, "fe: i_clusters = %u, i_size=%llu\n",
  544. le32_to_cpu(fe->i_clusters),
  545. (unsigned long long)le64_to_cpu(fe->i_size));
  546. mlog(0, "inode: ip_clusters=%u, i_size=%lld\n",
  547. OCFS2_I(inode)->ip_clusters, (long long)i_size_read(inode));
  548. leave:
  549. if (handle) {
  550. ocfs2_commit_trans(osb, handle);
  551. handle = NULL;
  552. }
  553. if (data_ac) {
  554. ocfs2_free_alloc_context(data_ac);
  555. data_ac = NULL;
  556. }
  557. if (meta_ac) {
  558. ocfs2_free_alloc_context(meta_ac);
  559. meta_ac = NULL;
  560. }
  561. if ((!status) && restart_func) {
  562. restart_func = 0;
  563. goto restart_all;
  564. }
  565. if (bh) {
  566. brelse(bh);
  567. bh = NULL;
  568. }
  569. mlog_exit(status);
  570. return status;
  571. }
  572. /* Some parts of this taken from generic_cont_expand, which turned out
  573. * to be too fragile to do exactly what we need without us having to
  574. * worry about recursive locking in ->prepare_write() and
  575. * ->commit_write(). */
  576. static int ocfs2_write_zero_page(struct inode *inode,
  577. u64 size)
  578. {
  579. struct address_space *mapping = inode->i_mapping;
  580. struct page *page;
  581. unsigned long index;
  582. unsigned int offset;
  583. handle_t *handle = NULL;
  584. int ret;
  585. offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */
  586. /* ugh. in prepare/commit_write, if from==to==start of block, we
  587. ** skip the prepare. make sure we never send an offset for the start
  588. ** of a block
  589. */
  590. if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) {
  591. offset++;
  592. }
  593. index = size >> PAGE_CACHE_SHIFT;
  594. page = grab_cache_page(mapping, index);
  595. if (!page) {
  596. ret = -ENOMEM;
  597. mlog_errno(ret);
  598. goto out;
  599. }
  600. ret = ocfs2_prepare_write_nolock(inode, page, offset, offset);
  601. if (ret < 0) {
  602. mlog_errno(ret);
  603. goto out_unlock;
  604. }
  605. if (ocfs2_should_order_data(inode)) {
  606. handle = ocfs2_start_walk_page_trans(inode, page, offset,
  607. offset);
  608. if (IS_ERR(handle)) {
  609. ret = PTR_ERR(handle);
  610. handle = NULL;
  611. goto out_unlock;
  612. }
  613. }
  614. /* must not update i_size! */
  615. ret = block_commit_write(page, offset, offset);
  616. if (ret < 0)
  617. mlog_errno(ret);
  618. else
  619. ret = 0;
  620. if (handle)
  621. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  622. out_unlock:
  623. unlock_page(page);
  624. page_cache_release(page);
  625. out:
  626. return ret;
  627. }
  628. static int ocfs2_zero_extend(struct inode *inode,
  629. u64 zero_to_size)
  630. {
  631. int ret = 0;
  632. u64 start_off;
  633. struct super_block *sb = inode->i_sb;
  634. start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
  635. while (start_off < zero_to_size) {
  636. ret = ocfs2_write_zero_page(inode, start_off);
  637. if (ret < 0) {
  638. mlog_errno(ret);
  639. goto out;
  640. }
  641. start_off += sb->s_blocksize;
  642. /*
  643. * Very large extends have the potential to lock up
  644. * the cpu for extended periods of time.
  645. */
  646. cond_resched();
  647. }
  648. out:
  649. return ret;
  650. }
  651. int ocfs2_extend_no_holes(struct inode *inode, u64 new_i_size, u64 zero_to)
  652. {
  653. int ret;
  654. u32 clusters_to_add;
  655. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  656. clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size);
  657. if (clusters_to_add < oi->ip_clusters)
  658. clusters_to_add = 0;
  659. else
  660. clusters_to_add -= oi->ip_clusters;
  661. if (clusters_to_add) {
  662. ret = __ocfs2_extend_allocation(inode, oi->ip_clusters,
  663. clusters_to_add, 0);
  664. if (ret) {
  665. mlog_errno(ret);
  666. goto out;
  667. }
  668. }
  669. /*
  670. * Call this even if we don't add any clusters to the tree. We
  671. * still need to zero the area between the old i_size and the
  672. * new i_size.
  673. */
  674. ret = ocfs2_zero_extend(inode, zero_to);
  675. if (ret < 0)
  676. mlog_errno(ret);
  677. out:
  678. return ret;
  679. }
  680. static int ocfs2_extend_file(struct inode *inode,
  681. struct buffer_head *di_bh,
  682. u64 new_i_size)
  683. {
  684. int ret = 0;
  685. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  686. BUG_ON(!di_bh);
  687. /* setattr sometimes calls us like this. */
  688. if (new_i_size == 0)
  689. goto out;
  690. if (i_size_read(inode) == new_i_size)
  691. goto out;
  692. BUG_ON(new_i_size < i_size_read(inode));
  693. /*
  694. * Fall through for converting inline data, even if the fs
  695. * supports sparse files.
  696. *
  697. * The check for inline data here is legal - nobody can add
  698. * the feature since we have i_mutex. We must check it again
  699. * after acquiring ip_alloc_sem though, as paths like mmap
  700. * might have raced us to converting the inode to extents.
  701. */
  702. if (!(oi->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  703. && ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
  704. goto out_update_size;
  705. /*
  706. * The alloc sem blocks people in read/write from reading our
  707. * allocation until we're done changing it. We depend on
  708. * i_mutex to block other extend/truncate calls while we're
  709. * here.
  710. */
  711. down_write(&oi->ip_alloc_sem);
  712. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  713. /*
  714. * We can optimize small extends by keeping the inodes
  715. * inline data.
  716. */
  717. if (ocfs2_size_fits_inline_data(di_bh, new_i_size)) {
  718. up_write(&oi->ip_alloc_sem);
  719. goto out_update_size;
  720. }
  721. ret = ocfs2_convert_inline_data_to_extents(inode, di_bh);
  722. if (ret) {
  723. up_write(&oi->ip_alloc_sem);
  724. mlog_errno(ret);
  725. goto out;
  726. }
  727. }
  728. if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
  729. ret = ocfs2_extend_no_holes(inode, new_i_size, new_i_size);
  730. up_write(&oi->ip_alloc_sem);
  731. if (ret < 0) {
  732. mlog_errno(ret);
  733. goto out;
  734. }
  735. out_update_size:
  736. ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
  737. if (ret < 0)
  738. mlog_errno(ret);
  739. out:
  740. return ret;
  741. }
  742. int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  743. {
  744. int status = 0, size_change;
  745. struct inode *inode = dentry->d_inode;
  746. struct super_block *sb = inode->i_sb;
  747. struct ocfs2_super *osb = OCFS2_SB(sb);
  748. struct buffer_head *bh = NULL;
  749. handle_t *handle = NULL;
  750. mlog_entry("(0x%p, '%.*s')\n", dentry,
  751. dentry->d_name.len, dentry->d_name.name);
  752. /* ensuring we don't even attempt to truncate a symlink */
  753. if (S_ISLNK(inode->i_mode))
  754. attr->ia_valid &= ~ATTR_SIZE;
  755. if (attr->ia_valid & ATTR_MODE)
  756. mlog(0, "mode change: %d\n", attr->ia_mode);
  757. if (attr->ia_valid & ATTR_UID)
  758. mlog(0, "uid change: %d\n", attr->ia_uid);
  759. if (attr->ia_valid & ATTR_GID)
  760. mlog(0, "gid change: %d\n", attr->ia_gid);
  761. if (attr->ia_valid & ATTR_SIZE)
  762. mlog(0, "size change...\n");
  763. if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME))
  764. mlog(0, "time change...\n");
  765. #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
  766. | ATTR_GID | ATTR_UID | ATTR_MODE)
  767. if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) {
  768. mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid);
  769. return 0;
  770. }
  771. status = inode_change_ok(inode, attr);
  772. if (status)
  773. return status;
  774. size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
  775. if (size_change) {
  776. status = ocfs2_rw_lock(inode, 1);
  777. if (status < 0) {
  778. mlog_errno(status);
  779. goto bail;
  780. }
  781. }
  782. status = ocfs2_inode_lock(inode, &bh, 1);
  783. if (status < 0) {
  784. if (status != -ENOENT)
  785. mlog_errno(status);
  786. goto bail_unlock_rw;
  787. }
  788. if (size_change && attr->ia_size != i_size_read(inode)) {
  789. if (attr->ia_size > sb->s_maxbytes) {
  790. status = -EFBIG;
  791. goto bail_unlock;
  792. }
  793. if (i_size_read(inode) > attr->ia_size)
  794. status = ocfs2_truncate_file(inode, bh, attr->ia_size);
  795. else
  796. status = ocfs2_extend_file(inode, bh, attr->ia_size);
  797. if (status < 0) {
  798. if (status != -ENOSPC)
  799. mlog_errno(status);
  800. status = -ENOSPC;
  801. goto bail_unlock;
  802. }
  803. }
  804. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  805. if (IS_ERR(handle)) {
  806. status = PTR_ERR(handle);
  807. mlog_errno(status);
  808. goto bail_unlock;
  809. }
  810. /*
  811. * This will intentionally not wind up calling vmtruncate(),
  812. * since all the work for a size change has been done above.
  813. * Otherwise, we could get into problems with truncate as
  814. * ip_alloc_sem is used there to protect against i_size
  815. * changes.
  816. */
  817. status = inode_setattr(inode, attr);
  818. if (status < 0) {
  819. mlog_errno(status);
  820. goto bail_commit;
  821. }
  822. status = ocfs2_mark_inode_dirty(handle, inode, bh);
  823. if (status < 0)
  824. mlog_errno(status);
  825. bail_commit:
  826. ocfs2_commit_trans(osb, handle);
  827. bail_unlock:
  828. ocfs2_inode_unlock(inode, 1);
  829. bail_unlock_rw:
  830. if (size_change)
  831. ocfs2_rw_unlock(inode, 1);
  832. bail:
  833. if (bh)
  834. brelse(bh);
  835. mlog_exit(status);
  836. return status;
  837. }
  838. int ocfs2_getattr(struct vfsmount *mnt,
  839. struct dentry *dentry,
  840. struct kstat *stat)
  841. {
  842. struct inode *inode = dentry->d_inode;
  843. struct super_block *sb = dentry->d_inode->i_sb;
  844. struct ocfs2_super *osb = sb->s_fs_info;
  845. int err;
  846. mlog_entry_void();
  847. err = ocfs2_inode_revalidate(dentry);
  848. if (err) {
  849. if (err != -ENOENT)
  850. mlog_errno(err);
  851. goto bail;
  852. }
  853. generic_fillattr(inode, stat);
  854. /* We set the blksize from the cluster size for performance */
  855. stat->blksize = osb->s_clustersize;
  856. bail:
  857. mlog_exit(err);
  858. return err;
  859. }
  860. int ocfs2_permission(struct inode *inode, int mask)
  861. {
  862. int ret;
  863. mlog_entry_void();
  864. ret = ocfs2_inode_lock(inode, NULL, 0);
  865. if (ret) {
  866. if (ret != -ENOENT)
  867. mlog_errno(ret);
  868. goto out;
  869. }
  870. ret = generic_permission(inode, mask, NULL);
  871. ocfs2_inode_unlock(inode, 0);
  872. out:
  873. mlog_exit(ret);
  874. return ret;
  875. }
  876. static int __ocfs2_write_remove_suid(struct inode *inode,
  877. struct buffer_head *bh)
  878. {
  879. int ret;
  880. handle_t *handle;
  881. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  882. struct ocfs2_dinode *di;
  883. mlog_entry("(Inode %llu, mode 0%o)\n",
  884. (unsigned long long)OCFS2_I(inode)->ip_blkno, inode->i_mode);
  885. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  886. if (handle == NULL) {
  887. ret = -ENOMEM;
  888. mlog_errno(ret);
  889. goto out;
  890. }
  891. ret = ocfs2_journal_access(handle, inode, bh,
  892. OCFS2_JOURNAL_ACCESS_WRITE);
  893. if (ret < 0) {
  894. mlog_errno(ret);
  895. goto out_trans;
  896. }
  897. inode->i_mode &= ~S_ISUID;
  898. if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
  899. inode->i_mode &= ~S_ISGID;
  900. di = (struct ocfs2_dinode *) bh->b_data;
  901. di->i_mode = cpu_to_le16(inode->i_mode);
  902. ret = ocfs2_journal_dirty(handle, bh);
  903. if (ret < 0)
  904. mlog_errno(ret);
  905. out_trans:
  906. ocfs2_commit_trans(osb, handle);
  907. out:
  908. mlog_exit(ret);
  909. return ret;
  910. }
  911. /*
  912. * Will look for holes and unwritten extents in the range starting at
  913. * pos for count bytes (inclusive).
  914. */
  915. static int ocfs2_check_range_for_holes(struct inode *inode, loff_t pos,
  916. size_t count)
  917. {
  918. int ret = 0;
  919. unsigned int extent_flags;
  920. u32 cpos, clusters, extent_len, phys_cpos;
  921. struct super_block *sb = inode->i_sb;
  922. cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
  923. clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
  924. while (clusters) {
  925. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
  926. &extent_flags);
  927. if (ret < 0) {
  928. mlog_errno(ret);
  929. goto out;
  930. }
  931. if (phys_cpos == 0 || (extent_flags & OCFS2_EXT_UNWRITTEN)) {
  932. ret = 1;
  933. break;
  934. }
  935. if (extent_len > clusters)
  936. extent_len = clusters;
  937. clusters -= extent_len;
  938. cpos += extent_len;
  939. }
  940. out:
  941. return ret;
  942. }
  943. static int ocfs2_write_remove_suid(struct inode *inode)
  944. {
  945. int ret;
  946. struct buffer_head *bh = NULL;
  947. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  948. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  949. oi->ip_blkno, &bh, OCFS2_BH_CACHED, inode);
  950. if (ret < 0) {
  951. mlog_errno(ret);
  952. goto out;
  953. }
  954. ret = __ocfs2_write_remove_suid(inode, bh);
  955. out:
  956. brelse(bh);
  957. return ret;
  958. }
  959. /*
  960. * Allocate enough extents to cover the region starting at byte offset
  961. * start for len bytes. Existing extents are skipped, any extents
  962. * added are marked as "unwritten".
  963. */
  964. static int ocfs2_allocate_unwritten_extents(struct inode *inode,
  965. u64 start, u64 len)
  966. {
  967. int ret;
  968. u32 cpos, phys_cpos, clusters, alloc_size;
  969. u64 end = start + len;
  970. struct buffer_head *di_bh = NULL;
  971. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  972. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  973. OCFS2_I(inode)->ip_blkno, &di_bh,
  974. OCFS2_BH_CACHED, inode);
  975. if (ret) {
  976. mlog_errno(ret);
  977. goto out;
  978. }
  979. /*
  980. * Nothing to do if the requested reservation range
  981. * fits within the inode.
  982. */
  983. if (ocfs2_size_fits_inline_data(di_bh, end))
  984. goto out;
  985. ret = ocfs2_convert_inline_data_to_extents(inode, di_bh);
  986. if (ret) {
  987. mlog_errno(ret);
  988. goto out;
  989. }
  990. }
  991. /*
  992. * We consider both start and len to be inclusive.
  993. */
  994. cpos = start >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  995. clusters = ocfs2_clusters_for_bytes(inode->i_sb, start + len);
  996. clusters -= cpos;
  997. while (clusters) {
  998. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos,
  999. &alloc_size, NULL);
  1000. if (ret) {
  1001. mlog_errno(ret);
  1002. goto out;
  1003. }
  1004. /*
  1005. * Hole or existing extent len can be arbitrary, so
  1006. * cap it to our own allocation request.
  1007. */
  1008. if (alloc_size > clusters)
  1009. alloc_size = clusters;
  1010. if (phys_cpos) {
  1011. /*
  1012. * We already have an allocation at this
  1013. * region so we can safely skip it.
  1014. */
  1015. goto next;
  1016. }
  1017. ret = __ocfs2_extend_allocation(inode, cpos, alloc_size, 1);
  1018. if (ret) {
  1019. if (ret != -ENOSPC)
  1020. mlog_errno(ret);
  1021. goto out;
  1022. }
  1023. next:
  1024. cpos += alloc_size;
  1025. clusters -= alloc_size;
  1026. }
  1027. ret = 0;
  1028. out:
  1029. brelse(di_bh);
  1030. return ret;
  1031. }
  1032. static int __ocfs2_remove_inode_range(struct inode *inode,
  1033. struct buffer_head *di_bh,
  1034. u32 cpos, u32 phys_cpos, u32 len,
  1035. struct ocfs2_cached_dealloc_ctxt *dealloc)
  1036. {
  1037. int ret;
  1038. u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
  1039. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1040. struct inode *tl_inode = osb->osb_tl_inode;
  1041. handle_t *handle;
  1042. struct ocfs2_alloc_context *meta_ac = NULL;
  1043. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1044. ret = ocfs2_lock_allocators(inode, di_bh, &di->id2.i_list,
  1045. 0, 1, NULL, &meta_ac,
  1046. OCFS2_DINODE_EXTENT, NULL);
  1047. if (ret) {
  1048. mlog_errno(ret);
  1049. return ret;
  1050. }
  1051. mutex_lock(&tl_inode->i_mutex);
  1052. if (ocfs2_truncate_log_needs_flush(osb)) {
  1053. ret = __ocfs2_flush_truncate_log(osb);
  1054. if (ret < 0) {
  1055. mlog_errno(ret);
  1056. goto out;
  1057. }
  1058. }
  1059. handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
  1060. if (handle == NULL) {
  1061. ret = -ENOMEM;
  1062. mlog_errno(ret);
  1063. goto out;
  1064. }
  1065. ret = ocfs2_journal_access(handle, inode, di_bh,
  1066. OCFS2_JOURNAL_ACCESS_WRITE);
  1067. if (ret) {
  1068. mlog_errno(ret);
  1069. goto out;
  1070. }
  1071. ret = ocfs2_remove_extent(inode, di_bh, cpos, len, handle, meta_ac,
  1072. dealloc, OCFS2_DINODE_EXTENT, NULL);
  1073. if (ret) {
  1074. mlog_errno(ret);
  1075. goto out_commit;
  1076. }
  1077. OCFS2_I(inode)->ip_clusters -= len;
  1078. di->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters);
  1079. ret = ocfs2_journal_dirty(handle, di_bh);
  1080. if (ret) {
  1081. mlog_errno(ret);
  1082. goto out_commit;
  1083. }
  1084. ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
  1085. if (ret)
  1086. mlog_errno(ret);
  1087. out_commit:
  1088. ocfs2_commit_trans(osb, handle);
  1089. out:
  1090. mutex_unlock(&tl_inode->i_mutex);
  1091. if (meta_ac)
  1092. ocfs2_free_alloc_context(meta_ac);
  1093. return ret;
  1094. }
  1095. /*
  1096. * Truncate a byte range, avoiding pages within partial clusters. This
  1097. * preserves those pages for the zeroing code to write to.
  1098. */
  1099. static void ocfs2_truncate_cluster_pages(struct inode *inode, u64 byte_start,
  1100. u64 byte_len)
  1101. {
  1102. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1103. loff_t start, end;
  1104. struct address_space *mapping = inode->i_mapping;
  1105. start = (loff_t)ocfs2_align_bytes_to_clusters(inode->i_sb, byte_start);
  1106. end = byte_start + byte_len;
  1107. end = end & ~(osb->s_clustersize - 1);
  1108. if (start < end) {
  1109. unmap_mapping_range(mapping, start, end - start, 0);
  1110. truncate_inode_pages_range(mapping, start, end - 1);
  1111. }
  1112. }
  1113. static int ocfs2_zero_partial_clusters(struct inode *inode,
  1114. u64 start, u64 len)
  1115. {
  1116. int ret = 0;
  1117. u64 tmpend, end = start + len;
  1118. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1119. unsigned int csize = osb->s_clustersize;
  1120. handle_t *handle;
  1121. /*
  1122. * The "start" and "end" values are NOT necessarily part of
  1123. * the range whose allocation is being deleted. Rather, this
  1124. * is what the user passed in with the request. We must zero
  1125. * partial clusters here. There's no need to worry about
  1126. * physical allocation - the zeroing code knows to skip holes.
  1127. */
  1128. mlog(0, "byte start: %llu, end: %llu\n",
  1129. (unsigned long long)start, (unsigned long long)end);
  1130. /*
  1131. * If both edges are on a cluster boundary then there's no
  1132. * zeroing required as the region is part of the allocation to
  1133. * be truncated.
  1134. */
  1135. if ((start & (csize - 1)) == 0 && (end & (csize - 1)) == 0)
  1136. goto out;
  1137. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1138. if (handle == NULL) {
  1139. ret = -ENOMEM;
  1140. mlog_errno(ret);
  1141. goto out;
  1142. }
  1143. /*
  1144. * We want to get the byte offset of the end of the 1st cluster.
  1145. */
  1146. tmpend = (u64)osb->s_clustersize + (start & ~(osb->s_clustersize - 1));
  1147. if (tmpend > end)
  1148. tmpend = end;
  1149. mlog(0, "1st range: start: %llu, tmpend: %llu\n",
  1150. (unsigned long long)start, (unsigned long long)tmpend);
  1151. ret = ocfs2_zero_range_for_truncate(inode, handle, start, tmpend);
  1152. if (ret)
  1153. mlog_errno(ret);
  1154. if (tmpend < end) {
  1155. /*
  1156. * This may make start and end equal, but the zeroing
  1157. * code will skip any work in that case so there's no
  1158. * need to catch it up here.
  1159. */
  1160. start = end & ~(osb->s_clustersize - 1);
  1161. mlog(0, "2nd range: start: %llu, end: %llu\n",
  1162. (unsigned long long)start, (unsigned long long)end);
  1163. ret = ocfs2_zero_range_for_truncate(inode, handle, start, end);
  1164. if (ret)
  1165. mlog_errno(ret);
  1166. }
  1167. ocfs2_commit_trans(osb, handle);
  1168. out:
  1169. return ret;
  1170. }
  1171. static int ocfs2_remove_inode_range(struct inode *inode,
  1172. struct buffer_head *di_bh, u64 byte_start,
  1173. u64 byte_len)
  1174. {
  1175. int ret = 0;
  1176. u32 trunc_start, trunc_len, cpos, phys_cpos, alloc_size;
  1177. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1178. struct ocfs2_cached_dealloc_ctxt dealloc;
  1179. struct address_space *mapping = inode->i_mapping;
  1180. ocfs2_init_dealloc_ctxt(&dealloc);
  1181. if (byte_len == 0)
  1182. return 0;
  1183. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1184. ret = ocfs2_truncate_inline(inode, di_bh, byte_start,
  1185. byte_start + byte_len, 0);
  1186. if (ret) {
  1187. mlog_errno(ret);
  1188. goto out;
  1189. }
  1190. /*
  1191. * There's no need to get fancy with the page cache
  1192. * truncate of an inline-data inode. We're talking
  1193. * about less than a page here, which will be cached
  1194. * in the dinode buffer anyway.
  1195. */
  1196. unmap_mapping_range(mapping, 0, 0, 0);
  1197. truncate_inode_pages(mapping, 0);
  1198. goto out;
  1199. }
  1200. trunc_start = ocfs2_clusters_for_bytes(osb->sb, byte_start);
  1201. trunc_len = (byte_start + byte_len) >> osb->s_clustersize_bits;
  1202. if (trunc_len >= trunc_start)
  1203. trunc_len -= trunc_start;
  1204. else
  1205. trunc_len = 0;
  1206. mlog(0, "Inode: %llu, start: %llu, len: %llu, cstart: %u, clen: %u\n",
  1207. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1208. (unsigned long long)byte_start,
  1209. (unsigned long long)byte_len, trunc_start, trunc_len);
  1210. ret = ocfs2_zero_partial_clusters(inode, byte_start, byte_len);
  1211. if (ret) {
  1212. mlog_errno(ret);
  1213. goto out;
  1214. }
  1215. cpos = trunc_start;
  1216. while (trunc_len) {
  1217. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos,
  1218. &alloc_size, NULL);
  1219. if (ret) {
  1220. mlog_errno(ret);
  1221. goto out;
  1222. }
  1223. if (alloc_size > trunc_len)
  1224. alloc_size = trunc_len;
  1225. /* Only do work for non-holes */
  1226. if (phys_cpos != 0) {
  1227. ret = __ocfs2_remove_inode_range(inode, di_bh, cpos,
  1228. phys_cpos, alloc_size,
  1229. &dealloc);
  1230. if (ret) {
  1231. mlog_errno(ret);
  1232. goto out;
  1233. }
  1234. }
  1235. cpos += alloc_size;
  1236. trunc_len -= alloc_size;
  1237. }
  1238. ocfs2_truncate_cluster_pages(inode, byte_start, byte_len);
  1239. out:
  1240. ocfs2_schedule_truncate_log_flush(osb, 1);
  1241. ocfs2_run_deallocs(osb, &dealloc);
  1242. return ret;
  1243. }
  1244. /*
  1245. * Parts of this function taken from xfs_change_file_space()
  1246. */
  1247. static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
  1248. loff_t f_pos, unsigned int cmd,
  1249. struct ocfs2_space_resv *sr,
  1250. int change_size)
  1251. {
  1252. int ret;
  1253. s64 llen;
  1254. loff_t size;
  1255. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1256. struct buffer_head *di_bh = NULL;
  1257. handle_t *handle;
  1258. unsigned long long max_off = inode->i_sb->s_maxbytes;
  1259. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  1260. return -EROFS;
  1261. mutex_lock(&inode->i_mutex);
  1262. /*
  1263. * This prevents concurrent writes on other nodes
  1264. */
  1265. ret = ocfs2_rw_lock(inode, 1);
  1266. if (ret) {
  1267. mlog_errno(ret);
  1268. goto out;
  1269. }
  1270. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  1271. if (ret) {
  1272. mlog_errno(ret);
  1273. goto out_rw_unlock;
  1274. }
  1275. if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
  1276. ret = -EPERM;
  1277. goto out_inode_unlock;
  1278. }
  1279. switch (sr->l_whence) {
  1280. case 0: /*SEEK_SET*/
  1281. break;
  1282. case 1: /*SEEK_CUR*/
  1283. sr->l_start += f_pos;
  1284. break;
  1285. case 2: /*SEEK_END*/
  1286. sr->l_start += i_size_read(inode);
  1287. break;
  1288. default:
  1289. ret = -EINVAL;
  1290. goto out_inode_unlock;
  1291. }
  1292. sr->l_whence = 0;
  1293. llen = sr->l_len > 0 ? sr->l_len - 1 : sr->l_len;
  1294. if (sr->l_start < 0
  1295. || sr->l_start > max_off
  1296. || (sr->l_start + llen) < 0
  1297. || (sr->l_start + llen) > max_off) {
  1298. ret = -EINVAL;
  1299. goto out_inode_unlock;
  1300. }
  1301. size = sr->l_start + sr->l_len;
  1302. if (cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) {
  1303. if (sr->l_len <= 0) {
  1304. ret = -EINVAL;
  1305. goto out_inode_unlock;
  1306. }
  1307. }
  1308. if (file && should_remove_suid(file->f_path.dentry)) {
  1309. ret = __ocfs2_write_remove_suid(inode, di_bh);
  1310. if (ret) {
  1311. mlog_errno(ret);
  1312. goto out_inode_unlock;
  1313. }
  1314. }
  1315. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  1316. switch (cmd) {
  1317. case OCFS2_IOC_RESVSP:
  1318. case OCFS2_IOC_RESVSP64:
  1319. /*
  1320. * This takes unsigned offsets, but the signed ones we
  1321. * pass have been checked against overflow above.
  1322. */
  1323. ret = ocfs2_allocate_unwritten_extents(inode, sr->l_start,
  1324. sr->l_len);
  1325. break;
  1326. case OCFS2_IOC_UNRESVSP:
  1327. case OCFS2_IOC_UNRESVSP64:
  1328. ret = ocfs2_remove_inode_range(inode, di_bh, sr->l_start,
  1329. sr->l_len);
  1330. break;
  1331. default:
  1332. ret = -EINVAL;
  1333. }
  1334. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  1335. if (ret) {
  1336. mlog_errno(ret);
  1337. goto out_inode_unlock;
  1338. }
  1339. /*
  1340. * We update c/mtime for these changes
  1341. */
  1342. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1343. if (IS_ERR(handle)) {
  1344. ret = PTR_ERR(handle);
  1345. mlog_errno(ret);
  1346. goto out_inode_unlock;
  1347. }
  1348. if (change_size && i_size_read(inode) < size)
  1349. i_size_write(inode, size);
  1350. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  1351. ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
  1352. if (ret < 0)
  1353. mlog_errno(ret);
  1354. ocfs2_commit_trans(osb, handle);
  1355. out_inode_unlock:
  1356. brelse(di_bh);
  1357. ocfs2_inode_unlock(inode, 1);
  1358. out_rw_unlock:
  1359. ocfs2_rw_unlock(inode, 1);
  1360. out:
  1361. mutex_unlock(&inode->i_mutex);
  1362. return ret;
  1363. }
  1364. int ocfs2_change_file_space(struct file *file, unsigned int cmd,
  1365. struct ocfs2_space_resv *sr)
  1366. {
  1367. struct inode *inode = file->f_path.dentry->d_inode;
  1368. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);;
  1369. if ((cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) &&
  1370. !ocfs2_writes_unwritten_extents(osb))
  1371. return -ENOTTY;
  1372. else if ((cmd == OCFS2_IOC_UNRESVSP || cmd == OCFS2_IOC_UNRESVSP64) &&
  1373. !ocfs2_sparse_alloc(osb))
  1374. return -ENOTTY;
  1375. if (!S_ISREG(inode->i_mode))
  1376. return -EINVAL;
  1377. if (!(file->f_mode & FMODE_WRITE))
  1378. return -EBADF;
  1379. return __ocfs2_change_file_space(file, inode, file->f_pos, cmd, sr, 0);
  1380. }
  1381. static long ocfs2_fallocate(struct inode *inode, int mode, loff_t offset,
  1382. loff_t len)
  1383. {
  1384. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1385. struct ocfs2_space_resv sr;
  1386. int change_size = 1;
  1387. if (!ocfs2_writes_unwritten_extents(osb))
  1388. return -EOPNOTSUPP;
  1389. if (S_ISDIR(inode->i_mode))
  1390. return -ENODEV;
  1391. if (mode & FALLOC_FL_KEEP_SIZE)
  1392. change_size = 0;
  1393. sr.l_whence = 0;
  1394. sr.l_start = (s64)offset;
  1395. sr.l_len = (s64)len;
  1396. return __ocfs2_change_file_space(NULL, inode, offset,
  1397. OCFS2_IOC_RESVSP64, &sr, change_size);
  1398. }
  1399. static int ocfs2_prepare_inode_for_write(struct dentry *dentry,
  1400. loff_t *ppos,
  1401. size_t count,
  1402. int appending,
  1403. int *direct_io)
  1404. {
  1405. int ret = 0, meta_level = 0;
  1406. struct inode *inode = dentry->d_inode;
  1407. loff_t saved_pos, end;
  1408. /*
  1409. * We start with a read level meta lock and only jump to an ex
  1410. * if we need to make modifications here.
  1411. */
  1412. for(;;) {
  1413. ret = ocfs2_inode_lock(inode, NULL, meta_level);
  1414. if (ret < 0) {
  1415. meta_level = -1;
  1416. mlog_errno(ret);
  1417. goto out;
  1418. }
  1419. /* Clear suid / sgid if necessary. We do this here
  1420. * instead of later in the write path because
  1421. * remove_suid() calls ->setattr without any hint that
  1422. * we may have already done our cluster locking. Since
  1423. * ocfs2_setattr() *must* take cluster locks to
  1424. * proceeed, this will lead us to recursively lock the
  1425. * inode. There's also the dinode i_size state which
  1426. * can be lost via setattr during extending writes (we
  1427. * set inode->i_size at the end of a write. */
  1428. if (should_remove_suid(dentry)) {
  1429. if (meta_level == 0) {
  1430. ocfs2_inode_unlock(inode, meta_level);
  1431. meta_level = 1;
  1432. continue;
  1433. }
  1434. ret = ocfs2_write_remove_suid(inode);
  1435. if (ret < 0) {
  1436. mlog_errno(ret);
  1437. goto out_unlock;
  1438. }
  1439. }
  1440. /* work on a copy of ppos until we're sure that we won't have
  1441. * to recalculate it due to relocking. */
  1442. if (appending) {
  1443. saved_pos = i_size_read(inode);
  1444. mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos);
  1445. } else {
  1446. saved_pos = *ppos;
  1447. }
  1448. end = saved_pos + count;
  1449. /*
  1450. * Skip the O_DIRECT checks if we don't need
  1451. * them.
  1452. */
  1453. if (!direct_io || !(*direct_io))
  1454. break;
  1455. /*
  1456. * There's no sane way to do direct writes to an inode
  1457. * with inline data.
  1458. */
  1459. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1460. *direct_io = 0;
  1461. break;
  1462. }
  1463. /*
  1464. * Allowing concurrent direct writes means
  1465. * i_size changes wouldn't be synchronized, so
  1466. * one node could wind up truncating another
  1467. * nodes writes.
  1468. */
  1469. if (end > i_size_read(inode)) {
  1470. *direct_io = 0;
  1471. break;
  1472. }
  1473. /*
  1474. * We don't fill holes during direct io, so
  1475. * check for them here. If any are found, the
  1476. * caller will have to retake some cluster
  1477. * locks and initiate the io as buffered.
  1478. */
  1479. ret = ocfs2_check_range_for_holes(inode, saved_pos, count);
  1480. if (ret == 1) {
  1481. *direct_io = 0;
  1482. ret = 0;
  1483. } else if (ret < 0)
  1484. mlog_errno(ret);
  1485. break;
  1486. }
  1487. if (appending)
  1488. *ppos = saved_pos;
  1489. out_unlock:
  1490. ocfs2_inode_unlock(inode, meta_level);
  1491. out:
  1492. return ret;
  1493. }
  1494. static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
  1495. const struct iovec *iov,
  1496. unsigned long nr_segs,
  1497. loff_t pos)
  1498. {
  1499. int ret, direct_io, appending, rw_level, have_alloc_sem = 0;
  1500. int can_do_direct;
  1501. ssize_t written = 0;
  1502. size_t ocount; /* original count */
  1503. size_t count; /* after file limit checks */
  1504. loff_t old_size, *ppos = &iocb->ki_pos;
  1505. u32 old_clusters;
  1506. struct file *file = iocb->ki_filp;
  1507. struct inode *inode = file->f_path.dentry->d_inode;
  1508. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1509. mlog_entry("(0x%p, %u, '%.*s')\n", file,
  1510. (unsigned int)nr_segs,
  1511. file->f_path.dentry->d_name.len,
  1512. file->f_path.dentry->d_name.name);
  1513. if (iocb->ki_left == 0)
  1514. return 0;
  1515. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1516. appending = file->f_flags & O_APPEND ? 1 : 0;
  1517. direct_io = file->f_flags & O_DIRECT ? 1 : 0;
  1518. mutex_lock(&inode->i_mutex);
  1519. relock:
  1520. /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */
  1521. if (direct_io) {
  1522. down_read(&inode->i_alloc_sem);
  1523. have_alloc_sem = 1;
  1524. }
  1525. /* concurrent O_DIRECT writes are allowed */
  1526. rw_level = !direct_io;
  1527. ret = ocfs2_rw_lock(inode, rw_level);
  1528. if (ret < 0) {
  1529. mlog_errno(ret);
  1530. goto out_sems;
  1531. }
  1532. can_do_direct = direct_io;
  1533. ret = ocfs2_prepare_inode_for_write(file->f_path.dentry, ppos,
  1534. iocb->ki_left, appending,
  1535. &can_do_direct);
  1536. if (ret < 0) {
  1537. mlog_errno(ret);
  1538. goto out;
  1539. }
  1540. /*
  1541. * We can't complete the direct I/O as requested, fall back to
  1542. * buffered I/O.
  1543. */
  1544. if (direct_io && !can_do_direct) {
  1545. ocfs2_rw_unlock(inode, rw_level);
  1546. up_read(&inode->i_alloc_sem);
  1547. have_alloc_sem = 0;
  1548. rw_level = -1;
  1549. direct_io = 0;
  1550. goto relock;
  1551. }
  1552. /*
  1553. * To later detect whether a journal commit for sync writes is
  1554. * necessary, we sample i_size, and cluster count here.
  1555. */
  1556. old_size = i_size_read(inode);
  1557. old_clusters = OCFS2_I(inode)->ip_clusters;
  1558. /* communicate with ocfs2_dio_end_io */
  1559. ocfs2_iocb_set_rw_locked(iocb, rw_level);
  1560. if (direct_io) {
  1561. ret = generic_segment_checks(iov, &nr_segs, &ocount,
  1562. VERIFY_READ);
  1563. if (ret)
  1564. goto out_dio;
  1565. ret = generic_write_checks(file, ppos, &count,
  1566. S_ISBLK(inode->i_mode));
  1567. if (ret)
  1568. goto out_dio;
  1569. written = generic_file_direct_write(iocb, iov, &nr_segs, *ppos,
  1570. ppos, count, ocount);
  1571. if (written < 0) {
  1572. ret = written;
  1573. goto out_dio;
  1574. }
  1575. } else {
  1576. written = generic_file_aio_write_nolock(iocb, iov, nr_segs,
  1577. *ppos);
  1578. }
  1579. out_dio:
  1580. /* buffered aio wouldn't have proper lock coverage today */
  1581. BUG_ON(ret == -EIOCBQUEUED && !(file->f_flags & O_DIRECT));
  1582. if ((file->f_flags & O_SYNC && !direct_io) || IS_SYNC(inode)) {
  1583. /*
  1584. * The generic write paths have handled getting data
  1585. * to disk, but since we don't make use of the dirty
  1586. * inode list, a manual journal commit is necessary
  1587. * here.
  1588. */
  1589. if (old_size != i_size_read(inode) ||
  1590. old_clusters != OCFS2_I(inode)->ip_clusters) {
  1591. ret = journal_force_commit(osb->journal->j_journal);
  1592. if (ret < 0)
  1593. written = ret;
  1594. }
  1595. }
  1596. /*
  1597. * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
  1598. * function pointer which is called when o_direct io completes so that
  1599. * it can unlock our rw lock. (it's the clustered equivalent of
  1600. * i_alloc_sem; protects truncate from racing with pending ios).
  1601. * Unfortunately there are error cases which call end_io and others
  1602. * that don't. so we don't have to unlock the rw_lock if either an
  1603. * async dio is going to do it in the future or an end_io after an
  1604. * error has already done it.
  1605. */
  1606. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  1607. rw_level = -1;
  1608. have_alloc_sem = 0;
  1609. }
  1610. out:
  1611. if (rw_level != -1)
  1612. ocfs2_rw_unlock(inode, rw_level);
  1613. out_sems:
  1614. if (have_alloc_sem)
  1615. up_read(&inode->i_alloc_sem);
  1616. mutex_unlock(&inode->i_mutex);
  1617. mlog_exit(ret);
  1618. return written ? written : ret;
  1619. }
  1620. static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe,
  1621. struct file *out,
  1622. loff_t *ppos,
  1623. size_t len,
  1624. unsigned int flags)
  1625. {
  1626. int ret;
  1627. struct inode *inode = out->f_path.dentry->d_inode;
  1628. mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", out, pipe,
  1629. (unsigned int)len,
  1630. out->f_path.dentry->d_name.len,
  1631. out->f_path.dentry->d_name.name);
  1632. inode_double_lock(inode, pipe->inode);
  1633. ret = ocfs2_rw_lock(inode, 1);
  1634. if (ret < 0) {
  1635. mlog_errno(ret);
  1636. goto out;
  1637. }
  1638. ret = ocfs2_prepare_inode_for_write(out->f_path.dentry, ppos, len, 0,
  1639. NULL);
  1640. if (ret < 0) {
  1641. mlog_errno(ret);
  1642. goto out_unlock;
  1643. }
  1644. ret = generic_file_splice_write_nolock(pipe, out, ppos, len, flags);
  1645. out_unlock:
  1646. ocfs2_rw_unlock(inode, 1);
  1647. out:
  1648. inode_double_unlock(inode, pipe->inode);
  1649. mlog_exit(ret);
  1650. return ret;
  1651. }
  1652. static ssize_t ocfs2_file_splice_read(struct file *in,
  1653. loff_t *ppos,
  1654. struct pipe_inode_info *pipe,
  1655. size_t len,
  1656. unsigned int flags)
  1657. {
  1658. int ret = 0;
  1659. struct inode *inode = in->f_path.dentry->d_inode;
  1660. mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", in, pipe,
  1661. (unsigned int)len,
  1662. in->f_path.dentry->d_name.len,
  1663. in->f_path.dentry->d_name.name);
  1664. /*
  1665. * See the comment in ocfs2_file_aio_read()
  1666. */
  1667. ret = ocfs2_inode_lock(inode, NULL, 0);
  1668. if (ret < 0) {
  1669. mlog_errno(ret);
  1670. goto bail;
  1671. }
  1672. ocfs2_inode_unlock(inode, 0);
  1673. ret = generic_file_splice_read(in, ppos, pipe, len, flags);
  1674. bail:
  1675. mlog_exit(ret);
  1676. return ret;
  1677. }
  1678. static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
  1679. const struct iovec *iov,
  1680. unsigned long nr_segs,
  1681. loff_t pos)
  1682. {
  1683. int ret = 0, rw_level = -1, have_alloc_sem = 0, lock_level = 0;
  1684. struct file *filp = iocb->ki_filp;
  1685. struct inode *inode = filp->f_path.dentry->d_inode;
  1686. mlog_entry("(0x%p, %u, '%.*s')\n", filp,
  1687. (unsigned int)nr_segs,
  1688. filp->f_path.dentry->d_name.len,
  1689. filp->f_path.dentry->d_name.name);
  1690. if (!inode) {
  1691. ret = -EINVAL;
  1692. mlog_errno(ret);
  1693. goto bail;
  1694. }
  1695. /*
  1696. * buffered reads protect themselves in ->readpage(). O_DIRECT reads
  1697. * need locks to protect pending reads from racing with truncate.
  1698. */
  1699. if (filp->f_flags & O_DIRECT) {
  1700. down_read(&inode->i_alloc_sem);
  1701. have_alloc_sem = 1;
  1702. ret = ocfs2_rw_lock(inode, 0);
  1703. if (ret < 0) {
  1704. mlog_errno(ret);
  1705. goto bail;
  1706. }
  1707. rw_level = 0;
  1708. /* communicate with ocfs2_dio_end_io */
  1709. ocfs2_iocb_set_rw_locked(iocb, rw_level);
  1710. }
  1711. /*
  1712. * We're fine letting folks race truncates and extending
  1713. * writes with read across the cluster, just like they can
  1714. * locally. Hence no rw_lock during read.
  1715. *
  1716. * Take and drop the meta data lock to update inode fields
  1717. * like i_size. This allows the checks down below
  1718. * generic_file_aio_read() a chance of actually working.
  1719. */
  1720. ret = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
  1721. if (ret < 0) {
  1722. mlog_errno(ret);
  1723. goto bail;
  1724. }
  1725. ocfs2_inode_unlock(inode, lock_level);
  1726. ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos);
  1727. if (ret == -EINVAL)
  1728. mlog(0, "generic_file_aio_read returned -EINVAL\n");
  1729. /* buffered aio wouldn't have proper lock coverage today */
  1730. BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
  1731. /* see ocfs2_file_aio_write */
  1732. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  1733. rw_level = -1;
  1734. have_alloc_sem = 0;
  1735. }
  1736. bail:
  1737. if (have_alloc_sem)
  1738. up_read(&inode->i_alloc_sem);
  1739. if (rw_level != -1)
  1740. ocfs2_rw_unlock(inode, rw_level);
  1741. mlog_exit(ret);
  1742. return ret;
  1743. }
  1744. const struct inode_operations ocfs2_file_iops = {
  1745. .setattr = ocfs2_setattr,
  1746. .getattr = ocfs2_getattr,
  1747. .permission = ocfs2_permission,
  1748. .fallocate = ocfs2_fallocate,
  1749. .fiemap = ocfs2_fiemap,
  1750. };
  1751. const struct inode_operations ocfs2_special_file_iops = {
  1752. .setattr = ocfs2_setattr,
  1753. .getattr = ocfs2_getattr,
  1754. .permission = ocfs2_permission,
  1755. };
  1756. /*
  1757. * Other than ->lock, keep ocfs2_fops and ocfs2_dops in sync with
  1758. * ocfs2_fops_no_plocks and ocfs2_dops_no_plocks!
  1759. */
  1760. const struct file_operations ocfs2_fops = {
  1761. .llseek = generic_file_llseek,
  1762. .read = do_sync_read,
  1763. .write = do_sync_write,
  1764. .mmap = ocfs2_mmap,
  1765. .fsync = ocfs2_sync_file,
  1766. .release = ocfs2_file_release,
  1767. .open = ocfs2_file_open,
  1768. .aio_read = ocfs2_file_aio_read,
  1769. .aio_write = ocfs2_file_aio_write,
  1770. .unlocked_ioctl = ocfs2_ioctl,
  1771. #ifdef CONFIG_COMPAT
  1772. .compat_ioctl = ocfs2_compat_ioctl,
  1773. #endif
  1774. .lock = ocfs2_lock,
  1775. .flock = ocfs2_flock,
  1776. .splice_read = ocfs2_file_splice_read,
  1777. .splice_write = ocfs2_file_splice_write,
  1778. };
  1779. const struct file_operations ocfs2_dops = {
  1780. .llseek = generic_file_llseek,
  1781. .read = generic_read_dir,
  1782. .readdir = ocfs2_readdir,
  1783. .fsync = ocfs2_sync_file,
  1784. .release = ocfs2_dir_release,
  1785. .open = ocfs2_dir_open,
  1786. .unlocked_ioctl = ocfs2_ioctl,
  1787. #ifdef CONFIG_COMPAT
  1788. .compat_ioctl = ocfs2_compat_ioctl,
  1789. #endif
  1790. .lock = ocfs2_lock,
  1791. .flock = ocfs2_flock,
  1792. };
  1793. /*
  1794. * POSIX-lockless variants of our file_operations.
  1795. *
  1796. * These will be used if the underlying cluster stack does not support
  1797. * posix file locking, if the user passes the "localflocks" mount
  1798. * option, or if we have a local-only fs.
  1799. *
  1800. * ocfs2_flock is in here because all stacks handle UNIX file locks,
  1801. * so we still want it in the case of no stack support for
  1802. * plocks. Internally, it will do the right thing when asked to ignore
  1803. * the cluster.
  1804. */
  1805. const struct file_operations ocfs2_fops_no_plocks = {
  1806. .llseek = generic_file_llseek,
  1807. .read = do_sync_read,
  1808. .write = do_sync_write,
  1809. .mmap = ocfs2_mmap,
  1810. .fsync = ocfs2_sync_file,
  1811. .release = ocfs2_file_release,
  1812. .open = ocfs2_file_open,
  1813. .aio_read = ocfs2_file_aio_read,
  1814. .aio_write = ocfs2_file_aio_write,
  1815. .unlocked_ioctl = ocfs2_ioctl,
  1816. #ifdef CONFIG_COMPAT
  1817. .compat_ioctl = ocfs2_compat_ioctl,
  1818. #endif
  1819. .flock = ocfs2_flock,
  1820. .splice_read = ocfs2_file_splice_read,
  1821. .splice_write = ocfs2_file_splice_write,
  1822. };
  1823. const struct file_operations ocfs2_dops_no_plocks = {
  1824. .llseek = generic_file_llseek,
  1825. .read = generic_read_dir,
  1826. .readdir = ocfs2_readdir,
  1827. .fsync = ocfs2_sync_file,
  1828. .release = ocfs2_dir_release,
  1829. .open = ocfs2_dir_open,
  1830. .unlocked_ioctl = ocfs2_ioctl,
  1831. #ifdef CONFIG_COMPAT
  1832. .compat_ioctl = ocfs2_compat_ioctl,
  1833. #endif
  1834. .flock = ocfs2_flock,
  1835. };