file.c 56 KB

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