file.c 53 KB

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