file.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789
  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. #include <linux/blkdev.h>
  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 "ocfs2_trace.h"
  60. #include "buffer_head_io.h"
  61. static int ocfs2_init_file_private(struct inode *inode, struct file *file)
  62. {
  63. struct ocfs2_file_private *fp;
  64. fp = kzalloc(sizeof(struct ocfs2_file_private), GFP_KERNEL);
  65. if (!fp)
  66. return -ENOMEM;
  67. fp->fp_file = file;
  68. mutex_init(&fp->fp_mutex);
  69. ocfs2_file_lock_res_init(&fp->fp_flock, fp);
  70. file->private_data = fp;
  71. return 0;
  72. }
  73. static void ocfs2_free_file_private(struct inode *inode, struct file *file)
  74. {
  75. struct ocfs2_file_private *fp = file->private_data;
  76. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  77. if (fp) {
  78. ocfs2_simple_drop_lockres(osb, &fp->fp_flock);
  79. ocfs2_lock_res_free(&fp->fp_flock);
  80. kfree(fp);
  81. file->private_data = NULL;
  82. }
  83. }
  84. static int ocfs2_file_open(struct inode *inode, struct file *file)
  85. {
  86. int status;
  87. int mode = file->f_flags;
  88. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  89. trace_ocfs2_file_open(inode, file, file->f_path.dentry,
  90. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  91. file->f_path.dentry->d_name.len,
  92. file->f_path.dentry->d_name.name, mode);
  93. if (file->f_mode & FMODE_WRITE)
  94. dquot_initialize(inode);
  95. spin_lock(&oi->ip_lock);
  96. /* Check that the inode hasn't been wiped from disk by another
  97. * node. If it hasn't then we're safe as long as we hold the
  98. * spin lock until our increment of open count. */
  99. if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
  100. spin_unlock(&oi->ip_lock);
  101. status = -ENOENT;
  102. goto leave;
  103. }
  104. if (mode & O_DIRECT)
  105. oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT;
  106. oi->ip_open_count++;
  107. spin_unlock(&oi->ip_lock);
  108. status = ocfs2_init_file_private(inode, file);
  109. if (status) {
  110. /*
  111. * We want to set open count back if we're failing the
  112. * open.
  113. */
  114. spin_lock(&oi->ip_lock);
  115. oi->ip_open_count--;
  116. spin_unlock(&oi->ip_lock);
  117. }
  118. leave:
  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. spin_lock(&oi->ip_lock);
  125. if (!--oi->ip_open_count)
  126. oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT;
  127. trace_ocfs2_file_release(inode, file, file->f_path.dentry,
  128. oi->ip_blkno,
  129. file->f_path.dentry->d_name.len,
  130. file->f_path.dentry->d_name.name,
  131. oi->ip_open_count);
  132. spin_unlock(&oi->ip_lock);
  133. ocfs2_free_file_private(inode, file);
  134. return 0;
  135. }
  136. static int ocfs2_dir_open(struct inode *inode, struct file *file)
  137. {
  138. return ocfs2_init_file_private(inode, file);
  139. }
  140. static int ocfs2_dir_release(struct inode *inode, struct file *file)
  141. {
  142. ocfs2_free_file_private(inode, file);
  143. return 0;
  144. }
  145. static int ocfs2_sync_file(struct file *file, loff_t start, loff_t end,
  146. int datasync)
  147. {
  148. int err = 0;
  149. journal_t *journal;
  150. struct inode *inode = file->f_mapping->host;
  151. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  152. trace_ocfs2_sync_file(inode, file, file->f_path.dentry,
  153. OCFS2_I(inode)->ip_blkno,
  154. file->f_path.dentry->d_name.len,
  155. file->f_path.dentry->d_name.name,
  156. (unsigned long long)datasync);
  157. err = filemap_write_and_wait_range(inode->i_mapping, start, end);
  158. if (err)
  159. return err;
  160. /*
  161. * Probably don't need the i_mutex at all in here, just putting it here
  162. * to be consistent with how fsync used to be called, someone more
  163. * familiar with the fs could possibly remove it.
  164. */
  165. mutex_lock(&inode->i_mutex);
  166. if (datasync && !(inode->i_state & I_DIRTY_DATASYNC)) {
  167. /*
  168. * We still have to flush drive's caches to get data to the
  169. * platter
  170. */
  171. if (osb->s_mount_opt & OCFS2_MOUNT_BARRIER)
  172. blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
  173. goto bail;
  174. }
  175. journal = osb->journal->j_journal;
  176. err = jbd2_journal_force_commit(journal);
  177. bail:
  178. if (err)
  179. mlog_errno(err);
  180. mutex_unlock(&inode->i_mutex);
  181. return (err < 0) ? -EIO : 0;
  182. }
  183. int ocfs2_should_update_atime(struct inode *inode,
  184. struct vfsmount *vfsmnt)
  185. {
  186. struct timespec now;
  187. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  188. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  189. return 0;
  190. if ((inode->i_flags & S_NOATIME) ||
  191. ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode)))
  192. return 0;
  193. /*
  194. * We can be called with no vfsmnt structure - NFSD will
  195. * sometimes do this.
  196. *
  197. * Note that our action here is different than touch_atime() -
  198. * if we can't tell whether this is a noatime mount, then we
  199. * don't know whether to trust the value of s_atime_quantum.
  200. */
  201. if (vfsmnt == NULL)
  202. return 0;
  203. if ((vfsmnt->mnt_flags & MNT_NOATIME) ||
  204. ((vfsmnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
  205. return 0;
  206. if (vfsmnt->mnt_flags & MNT_RELATIME) {
  207. if ((timespec_compare(&inode->i_atime, &inode->i_mtime) <= 0) ||
  208. (timespec_compare(&inode->i_atime, &inode->i_ctime) <= 0))
  209. return 1;
  210. return 0;
  211. }
  212. now = CURRENT_TIME;
  213. if ((now.tv_sec - inode->i_atime.tv_sec <= osb->s_atime_quantum))
  214. return 0;
  215. else
  216. return 1;
  217. }
  218. int ocfs2_update_inode_atime(struct inode *inode,
  219. struct buffer_head *bh)
  220. {
  221. int ret;
  222. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  223. handle_t *handle;
  224. struct ocfs2_dinode *di = (struct ocfs2_dinode *) bh->b_data;
  225. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  226. if (IS_ERR(handle)) {
  227. ret = PTR_ERR(handle);
  228. mlog_errno(ret);
  229. goto out;
  230. }
  231. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
  232. OCFS2_JOURNAL_ACCESS_WRITE);
  233. if (ret) {
  234. mlog_errno(ret);
  235. goto out_commit;
  236. }
  237. /*
  238. * Don't use ocfs2_mark_inode_dirty() here as we don't always
  239. * have i_mutex to guard against concurrent changes to other
  240. * inode fields.
  241. */
  242. inode->i_atime = CURRENT_TIME;
  243. di->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
  244. di->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
  245. ocfs2_journal_dirty(handle, bh);
  246. out_commit:
  247. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  248. out:
  249. return ret;
  250. }
  251. static int ocfs2_set_inode_size(handle_t *handle,
  252. struct inode *inode,
  253. struct buffer_head *fe_bh,
  254. u64 new_i_size)
  255. {
  256. int status;
  257. i_size_write(inode, new_i_size);
  258. inode->i_blocks = ocfs2_inode_sector_count(inode);
  259. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  260. status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  261. if (status < 0) {
  262. mlog_errno(status);
  263. goto bail;
  264. }
  265. bail:
  266. return status;
  267. }
  268. int ocfs2_simple_size_update(struct inode *inode,
  269. struct buffer_head *di_bh,
  270. u64 new_i_size)
  271. {
  272. int ret;
  273. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  274. handle_t *handle = NULL;
  275. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  276. if (IS_ERR(handle)) {
  277. ret = PTR_ERR(handle);
  278. mlog_errno(ret);
  279. goto out;
  280. }
  281. ret = ocfs2_set_inode_size(handle, inode, di_bh,
  282. new_i_size);
  283. if (ret < 0)
  284. mlog_errno(ret);
  285. ocfs2_commit_trans(osb, handle);
  286. out:
  287. return ret;
  288. }
  289. static int ocfs2_cow_file_pos(struct inode *inode,
  290. struct buffer_head *fe_bh,
  291. u64 offset)
  292. {
  293. int status;
  294. u32 phys, cpos = offset >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  295. unsigned int num_clusters = 0;
  296. unsigned int ext_flags = 0;
  297. /*
  298. * If the new offset is aligned to the range of the cluster, there is
  299. * no space for ocfs2_zero_range_for_truncate to fill, so no need to
  300. * CoW either.
  301. */
  302. if ((offset & (OCFS2_SB(inode->i_sb)->s_clustersize - 1)) == 0)
  303. return 0;
  304. status = ocfs2_get_clusters(inode, cpos, &phys,
  305. &num_clusters, &ext_flags);
  306. if (status) {
  307. mlog_errno(status);
  308. goto out;
  309. }
  310. if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
  311. goto out;
  312. return ocfs2_refcount_cow(inode, NULL, fe_bh, cpos, 1, cpos+1);
  313. out:
  314. return status;
  315. }
  316. static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
  317. struct inode *inode,
  318. struct buffer_head *fe_bh,
  319. u64 new_i_size)
  320. {
  321. int status;
  322. handle_t *handle;
  323. struct ocfs2_dinode *di;
  324. u64 cluster_bytes;
  325. /*
  326. * We need to CoW the cluster contains the offset if it is reflinked
  327. * since we will call ocfs2_zero_range_for_truncate later which will
  328. * write "0" from offset to the end of the cluster.
  329. */
  330. status = ocfs2_cow_file_pos(inode, fe_bh, new_i_size);
  331. if (status) {
  332. mlog_errno(status);
  333. return status;
  334. }
  335. /* TODO: This needs to actually orphan the inode in this
  336. * transaction. */
  337. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  338. if (IS_ERR(handle)) {
  339. status = PTR_ERR(handle);
  340. mlog_errno(status);
  341. goto out;
  342. }
  343. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
  344. OCFS2_JOURNAL_ACCESS_WRITE);
  345. if (status < 0) {
  346. mlog_errno(status);
  347. goto out_commit;
  348. }
  349. /*
  350. * Do this before setting i_size.
  351. */
  352. cluster_bytes = ocfs2_align_bytes_to_clusters(inode->i_sb, new_i_size);
  353. status = ocfs2_zero_range_for_truncate(inode, handle, new_i_size,
  354. cluster_bytes);
  355. if (status) {
  356. mlog_errno(status);
  357. goto out_commit;
  358. }
  359. i_size_write(inode, new_i_size);
  360. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  361. di = (struct ocfs2_dinode *) fe_bh->b_data;
  362. di->i_size = cpu_to_le64(new_i_size);
  363. di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
  364. di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  365. ocfs2_journal_dirty(handle, fe_bh);
  366. out_commit:
  367. ocfs2_commit_trans(osb, handle);
  368. out:
  369. return status;
  370. }
  371. static int ocfs2_truncate_file(struct inode *inode,
  372. struct buffer_head *di_bh,
  373. u64 new_i_size)
  374. {
  375. int status = 0;
  376. struct ocfs2_dinode *fe = NULL;
  377. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  378. /* We trust di_bh because it comes from ocfs2_inode_lock(), which
  379. * already validated it */
  380. fe = (struct ocfs2_dinode *) di_bh->b_data;
  381. trace_ocfs2_truncate_file((unsigned long long)OCFS2_I(inode)->ip_blkno,
  382. (unsigned long long)le64_to_cpu(fe->i_size),
  383. (unsigned long long)new_i_size);
  384. mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode),
  385. "Inode %llu, inode i_size = %lld != di "
  386. "i_size = %llu, i_flags = 0x%x\n",
  387. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  388. i_size_read(inode),
  389. (unsigned long long)le64_to_cpu(fe->i_size),
  390. le32_to_cpu(fe->i_flags));
  391. if (new_i_size > le64_to_cpu(fe->i_size)) {
  392. trace_ocfs2_truncate_file_error(
  393. (unsigned long long)le64_to_cpu(fe->i_size),
  394. (unsigned long long)new_i_size);
  395. status = -EINVAL;
  396. mlog_errno(status);
  397. goto bail;
  398. }
  399. /* lets handle the simple truncate cases before doing any more
  400. * cluster locking. */
  401. if (new_i_size == le64_to_cpu(fe->i_size))
  402. goto bail;
  403. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  404. ocfs2_resv_discard(&osb->osb_la_resmap,
  405. &OCFS2_I(inode)->ip_la_data_resv);
  406. /*
  407. * The inode lock forced other nodes to sync and drop their
  408. * pages, which (correctly) happens even if we have a truncate
  409. * without allocation change - ocfs2 cluster sizes can be much
  410. * greater than page size, so we have to truncate them
  411. * anyway.
  412. */
  413. unmap_mapping_range(inode->i_mapping, new_i_size + PAGE_SIZE - 1, 0, 1);
  414. truncate_inode_pages(inode->i_mapping, new_i_size);
  415. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  416. status = ocfs2_truncate_inline(inode, di_bh, new_i_size,
  417. i_size_read(inode), 1);
  418. if (status)
  419. mlog_errno(status);
  420. goto bail_unlock_sem;
  421. }
  422. /* alright, we're going to need to do a full blown alloc size
  423. * change. Orphan the inode so that recovery can complete the
  424. * truncate if necessary. This does the task of marking
  425. * i_size. */
  426. status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size);
  427. if (status < 0) {
  428. mlog_errno(status);
  429. goto bail_unlock_sem;
  430. }
  431. status = ocfs2_commit_truncate(osb, inode, di_bh);
  432. if (status < 0) {
  433. mlog_errno(status);
  434. goto bail_unlock_sem;
  435. }
  436. /* TODO: orphan dir cleanup here. */
  437. bail_unlock_sem:
  438. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  439. bail:
  440. if (!status && OCFS2_I(inode)->ip_clusters == 0)
  441. status = ocfs2_try_remove_refcount_tree(inode, di_bh);
  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. /*
  490. * This function only exists for file systems which don't
  491. * support holes.
  492. */
  493. BUG_ON(mark_unwritten && !ocfs2_sparse_alloc(osb));
  494. status = ocfs2_read_inode_block(inode, &bh);
  495. if (status < 0) {
  496. mlog_errno(status);
  497. goto leave;
  498. }
  499. fe = (struct ocfs2_dinode *) bh->b_data;
  500. restart_all:
  501. BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
  502. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), bh);
  503. status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
  504. &data_ac, &meta_ac);
  505. if (status) {
  506. mlog_errno(status);
  507. goto leave;
  508. }
  509. credits = ocfs2_calc_extend_credits(osb->sb, &fe->id2.i_list,
  510. clusters_to_add);
  511. handle = ocfs2_start_trans(osb, credits);
  512. if (IS_ERR(handle)) {
  513. status = PTR_ERR(handle);
  514. handle = NULL;
  515. mlog_errno(status);
  516. goto leave;
  517. }
  518. restarted_transaction:
  519. trace_ocfs2_extend_allocation(
  520. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  521. (unsigned long long)i_size_read(inode),
  522. le32_to_cpu(fe->i_clusters), clusters_to_add,
  523. why, restart_func);
  524. status = dquot_alloc_space_nodirty(inode,
  525. ocfs2_clusters_to_bytes(osb->sb, clusters_to_add));
  526. if (status)
  527. goto leave;
  528. did_quota = 1;
  529. /* reserve a write to the file entry early on - that we if we
  530. * run out of credits in the allocation path, we can still
  531. * update i_size. */
  532. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
  533. OCFS2_JOURNAL_ACCESS_WRITE);
  534. if (status < 0) {
  535. mlog_errno(status);
  536. goto leave;
  537. }
  538. prev_clusters = OCFS2_I(inode)->ip_clusters;
  539. status = ocfs2_add_inode_data(osb,
  540. inode,
  541. &logical_start,
  542. clusters_to_add,
  543. mark_unwritten,
  544. bh,
  545. handle,
  546. data_ac,
  547. meta_ac,
  548. &why);
  549. if ((status < 0) && (status != -EAGAIN)) {
  550. if (status != -ENOSPC)
  551. mlog_errno(status);
  552. goto leave;
  553. }
  554. ocfs2_journal_dirty(handle, bh);
  555. spin_lock(&OCFS2_I(inode)->ip_lock);
  556. clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters);
  557. spin_unlock(&OCFS2_I(inode)->ip_lock);
  558. /* Release unused quota reservation */
  559. dquot_free_space(inode,
  560. ocfs2_clusters_to_bytes(osb->sb, clusters_to_add));
  561. did_quota = 0;
  562. if (why != RESTART_NONE && clusters_to_add) {
  563. if (why == RESTART_META) {
  564. restart_func = 1;
  565. status = 0;
  566. } else {
  567. BUG_ON(why != RESTART_TRANS);
  568. /* TODO: This can be more intelligent. */
  569. credits = ocfs2_calc_extend_credits(osb->sb,
  570. &fe->id2.i_list,
  571. clusters_to_add);
  572. status = ocfs2_extend_trans(handle, credits);
  573. if (status < 0) {
  574. /* handle still has to be committed at
  575. * this point. */
  576. status = -ENOMEM;
  577. mlog_errno(status);
  578. goto leave;
  579. }
  580. goto restarted_transaction;
  581. }
  582. }
  583. trace_ocfs2_extend_allocation_end(OCFS2_I(inode)->ip_blkno,
  584. le32_to_cpu(fe->i_clusters),
  585. (unsigned long long)le64_to_cpu(fe->i_size),
  586. OCFS2_I(inode)->ip_clusters,
  587. (unsigned long long)i_size_read(inode));
  588. leave:
  589. if (status < 0 && did_quota)
  590. dquot_free_space(inode,
  591. ocfs2_clusters_to_bytes(osb->sb, clusters_to_add));
  592. if (handle) {
  593. ocfs2_commit_trans(osb, handle);
  594. handle = NULL;
  595. }
  596. if (data_ac) {
  597. ocfs2_free_alloc_context(data_ac);
  598. data_ac = NULL;
  599. }
  600. if (meta_ac) {
  601. ocfs2_free_alloc_context(meta_ac);
  602. meta_ac = NULL;
  603. }
  604. if ((!status) && restart_func) {
  605. restart_func = 0;
  606. goto restart_all;
  607. }
  608. brelse(bh);
  609. bh = NULL;
  610. return status;
  611. }
  612. /*
  613. * While a write will already be ordering the data, a truncate will not.
  614. * Thus, we need to explicitly order the zeroed pages.
  615. */
  616. static handle_t *ocfs2_zero_start_ordered_transaction(struct inode *inode)
  617. {
  618. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  619. handle_t *handle = NULL;
  620. int ret = 0;
  621. if (!ocfs2_should_order_data(inode))
  622. goto out;
  623. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  624. if (IS_ERR(handle)) {
  625. ret = -ENOMEM;
  626. mlog_errno(ret);
  627. goto out;
  628. }
  629. ret = ocfs2_jbd2_file_inode(handle, inode);
  630. if (ret < 0)
  631. mlog_errno(ret);
  632. out:
  633. if (ret) {
  634. if (!IS_ERR(handle))
  635. ocfs2_commit_trans(osb, handle);
  636. handle = ERR_PTR(ret);
  637. }
  638. return handle;
  639. }
  640. /* Some parts of this taken from generic_cont_expand, which turned out
  641. * to be too fragile to do exactly what we need without us having to
  642. * worry about recursive locking in ->write_begin() and ->write_end(). */
  643. static int ocfs2_write_zero_page(struct inode *inode, u64 abs_from,
  644. u64 abs_to)
  645. {
  646. struct address_space *mapping = inode->i_mapping;
  647. struct page *page;
  648. unsigned long index = abs_from >> PAGE_CACHE_SHIFT;
  649. handle_t *handle = NULL;
  650. int ret = 0;
  651. unsigned zero_from, zero_to, block_start, block_end;
  652. BUG_ON(abs_from >= abs_to);
  653. BUG_ON(abs_to > (((u64)index + 1) << PAGE_CACHE_SHIFT));
  654. BUG_ON(abs_from & (inode->i_blkbits - 1));
  655. page = find_or_create_page(mapping, index, GFP_NOFS);
  656. if (!page) {
  657. ret = -ENOMEM;
  658. mlog_errno(ret);
  659. goto out;
  660. }
  661. /* Get the offsets within the page that we want to zero */
  662. zero_from = abs_from & (PAGE_CACHE_SIZE - 1);
  663. zero_to = abs_to & (PAGE_CACHE_SIZE - 1);
  664. if (!zero_to)
  665. zero_to = PAGE_CACHE_SIZE;
  666. trace_ocfs2_write_zero_page(
  667. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  668. (unsigned long long)abs_from,
  669. (unsigned long long)abs_to,
  670. index, zero_from, zero_to);
  671. /* We know that zero_from is block aligned */
  672. for (block_start = zero_from; block_start < zero_to;
  673. block_start = block_end) {
  674. block_end = block_start + (1 << inode->i_blkbits);
  675. /*
  676. * block_start is block-aligned. Bump it by one to force
  677. * __block_write_begin and block_commit_write to zero the
  678. * whole block.
  679. */
  680. ret = __block_write_begin(page, block_start + 1, 0,
  681. ocfs2_get_block);
  682. if (ret < 0) {
  683. mlog_errno(ret);
  684. goto out_unlock;
  685. }
  686. if (!handle) {
  687. handle = ocfs2_zero_start_ordered_transaction(inode);
  688. if (IS_ERR(handle)) {
  689. ret = PTR_ERR(handle);
  690. handle = NULL;
  691. break;
  692. }
  693. }
  694. /* must not update i_size! */
  695. ret = block_commit_write(page, block_start + 1,
  696. block_start + 1);
  697. if (ret < 0)
  698. mlog_errno(ret);
  699. else
  700. ret = 0;
  701. }
  702. if (handle)
  703. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  704. out_unlock:
  705. unlock_page(page);
  706. page_cache_release(page);
  707. out:
  708. return ret;
  709. }
  710. /*
  711. * Find the next range to zero. We do this in terms of bytes because
  712. * that's what ocfs2_zero_extend() wants, and it is dealing with the
  713. * pagecache. We may return multiple extents.
  714. *
  715. * zero_start and zero_end are ocfs2_zero_extend()s current idea of what
  716. * needs to be zeroed. range_start and range_end return the next zeroing
  717. * range. A subsequent call should pass the previous range_end as its
  718. * zero_start. If range_end is 0, there's nothing to do.
  719. *
  720. * Unwritten extents are skipped over. Refcounted extents are CoWd.
  721. */
  722. static int ocfs2_zero_extend_get_range(struct inode *inode,
  723. struct buffer_head *di_bh,
  724. u64 zero_start, u64 zero_end,
  725. u64 *range_start, u64 *range_end)
  726. {
  727. int rc = 0, needs_cow = 0;
  728. u32 p_cpos, zero_clusters = 0;
  729. u32 zero_cpos =
  730. zero_start >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  731. u32 last_cpos = ocfs2_clusters_for_bytes(inode->i_sb, zero_end);
  732. unsigned int num_clusters = 0;
  733. unsigned int ext_flags = 0;
  734. while (zero_cpos < last_cpos) {
  735. rc = ocfs2_get_clusters(inode, zero_cpos, &p_cpos,
  736. &num_clusters, &ext_flags);
  737. if (rc) {
  738. mlog_errno(rc);
  739. goto out;
  740. }
  741. if (p_cpos && !(ext_flags & OCFS2_EXT_UNWRITTEN)) {
  742. zero_clusters = num_clusters;
  743. if (ext_flags & OCFS2_EXT_REFCOUNTED)
  744. needs_cow = 1;
  745. break;
  746. }
  747. zero_cpos += num_clusters;
  748. }
  749. if (!zero_clusters) {
  750. *range_end = 0;
  751. goto out;
  752. }
  753. while ((zero_cpos + zero_clusters) < last_cpos) {
  754. rc = ocfs2_get_clusters(inode, zero_cpos + zero_clusters,
  755. &p_cpos, &num_clusters,
  756. &ext_flags);
  757. if (rc) {
  758. mlog_errno(rc);
  759. goto out;
  760. }
  761. if (!p_cpos || (ext_flags & OCFS2_EXT_UNWRITTEN))
  762. break;
  763. if (ext_flags & OCFS2_EXT_REFCOUNTED)
  764. needs_cow = 1;
  765. zero_clusters += num_clusters;
  766. }
  767. if ((zero_cpos + zero_clusters) > last_cpos)
  768. zero_clusters = last_cpos - zero_cpos;
  769. if (needs_cow) {
  770. rc = ocfs2_refcount_cow(inode, NULL, di_bh, zero_cpos,
  771. zero_clusters, UINT_MAX);
  772. if (rc) {
  773. mlog_errno(rc);
  774. goto out;
  775. }
  776. }
  777. *range_start = ocfs2_clusters_to_bytes(inode->i_sb, zero_cpos);
  778. *range_end = ocfs2_clusters_to_bytes(inode->i_sb,
  779. zero_cpos + zero_clusters);
  780. out:
  781. return rc;
  782. }
  783. /*
  784. * Zero one range returned from ocfs2_zero_extend_get_range(). The caller
  785. * has made sure that the entire range needs zeroing.
  786. */
  787. static int ocfs2_zero_extend_range(struct inode *inode, u64 range_start,
  788. u64 range_end)
  789. {
  790. int rc = 0;
  791. u64 next_pos;
  792. u64 zero_pos = range_start;
  793. trace_ocfs2_zero_extend_range(
  794. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  795. (unsigned long long)range_start,
  796. (unsigned long long)range_end);
  797. BUG_ON(range_start >= range_end);
  798. while (zero_pos < range_end) {
  799. next_pos = (zero_pos & PAGE_CACHE_MASK) + PAGE_CACHE_SIZE;
  800. if (next_pos > range_end)
  801. next_pos = range_end;
  802. rc = ocfs2_write_zero_page(inode, zero_pos, next_pos);
  803. if (rc < 0) {
  804. mlog_errno(rc);
  805. break;
  806. }
  807. zero_pos = next_pos;
  808. /*
  809. * Very large extends have the potential to lock up
  810. * the cpu for extended periods of time.
  811. */
  812. cond_resched();
  813. }
  814. return rc;
  815. }
  816. int ocfs2_zero_extend(struct inode *inode, struct buffer_head *di_bh,
  817. loff_t zero_to_size)
  818. {
  819. int ret = 0;
  820. u64 zero_start, range_start = 0, range_end = 0;
  821. struct super_block *sb = inode->i_sb;
  822. zero_start = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
  823. trace_ocfs2_zero_extend((unsigned long long)OCFS2_I(inode)->ip_blkno,
  824. (unsigned long long)zero_start,
  825. (unsigned long long)i_size_read(inode));
  826. while (zero_start < zero_to_size) {
  827. ret = ocfs2_zero_extend_get_range(inode, di_bh, zero_start,
  828. zero_to_size,
  829. &range_start,
  830. &range_end);
  831. if (ret) {
  832. mlog_errno(ret);
  833. break;
  834. }
  835. if (!range_end)
  836. break;
  837. /* Trim the ends */
  838. if (range_start < zero_start)
  839. range_start = zero_start;
  840. if (range_end > zero_to_size)
  841. range_end = zero_to_size;
  842. ret = ocfs2_zero_extend_range(inode, range_start,
  843. range_end);
  844. if (ret) {
  845. mlog_errno(ret);
  846. break;
  847. }
  848. zero_start = range_end;
  849. }
  850. return ret;
  851. }
  852. int ocfs2_extend_no_holes(struct inode *inode, struct buffer_head *di_bh,
  853. u64 new_i_size, u64 zero_to)
  854. {
  855. int ret;
  856. u32 clusters_to_add;
  857. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  858. /*
  859. * Only quota files call this without a bh, and they can't be
  860. * refcounted.
  861. */
  862. BUG_ON(!di_bh && (oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
  863. BUG_ON(!di_bh && !(oi->ip_flags & OCFS2_INODE_SYSTEM_FILE));
  864. clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size);
  865. if (clusters_to_add < oi->ip_clusters)
  866. clusters_to_add = 0;
  867. else
  868. clusters_to_add -= oi->ip_clusters;
  869. if (clusters_to_add) {
  870. ret = __ocfs2_extend_allocation(inode, oi->ip_clusters,
  871. clusters_to_add, 0);
  872. if (ret) {
  873. mlog_errno(ret);
  874. goto out;
  875. }
  876. }
  877. /*
  878. * Call this even if we don't add any clusters to the tree. We
  879. * still need to zero the area between the old i_size and the
  880. * new i_size.
  881. */
  882. ret = ocfs2_zero_extend(inode, di_bh, zero_to);
  883. if (ret < 0)
  884. mlog_errno(ret);
  885. out:
  886. return ret;
  887. }
  888. static int ocfs2_extend_file(struct inode *inode,
  889. struct buffer_head *di_bh,
  890. u64 new_i_size)
  891. {
  892. int ret = 0;
  893. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  894. BUG_ON(!di_bh);
  895. /* setattr sometimes calls us like this. */
  896. if (new_i_size == 0)
  897. goto out;
  898. if (i_size_read(inode) == new_i_size)
  899. goto out;
  900. BUG_ON(new_i_size < i_size_read(inode));
  901. /*
  902. * The alloc sem blocks people in read/write from reading our
  903. * allocation until we're done changing it. We depend on
  904. * i_mutex to block other extend/truncate calls while we're
  905. * here. We even have to hold it for sparse files because there
  906. * might be some tail zeroing.
  907. */
  908. down_write(&oi->ip_alloc_sem);
  909. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  910. /*
  911. * We can optimize small extends by keeping the inodes
  912. * inline data.
  913. */
  914. if (ocfs2_size_fits_inline_data(di_bh, new_i_size)) {
  915. up_write(&oi->ip_alloc_sem);
  916. goto out_update_size;
  917. }
  918. ret = ocfs2_convert_inline_data_to_extents(inode, di_bh);
  919. if (ret) {
  920. up_write(&oi->ip_alloc_sem);
  921. mlog_errno(ret);
  922. goto out;
  923. }
  924. }
  925. if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
  926. ret = ocfs2_zero_extend(inode, di_bh, new_i_size);
  927. else
  928. ret = ocfs2_extend_no_holes(inode, di_bh, new_i_size,
  929. new_i_size);
  930. up_write(&oi->ip_alloc_sem);
  931. if (ret < 0) {
  932. mlog_errno(ret);
  933. goto out;
  934. }
  935. out_update_size:
  936. ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
  937. if (ret < 0)
  938. mlog_errno(ret);
  939. out:
  940. return ret;
  941. }
  942. int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  943. {
  944. int status = 0, size_change;
  945. struct inode *inode = dentry->d_inode;
  946. struct super_block *sb = inode->i_sb;
  947. struct ocfs2_super *osb = OCFS2_SB(sb);
  948. struct buffer_head *bh = NULL;
  949. handle_t *handle = NULL;
  950. struct dquot *transfer_to[MAXQUOTAS] = { };
  951. int qtype;
  952. trace_ocfs2_setattr(inode, dentry,
  953. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  954. dentry->d_name.len, dentry->d_name.name,
  955. attr->ia_valid, attr->ia_mode,
  956. attr->ia_uid, attr->ia_gid);
  957. /* ensuring we don't even attempt to truncate a symlink */
  958. if (S_ISLNK(inode->i_mode))
  959. attr->ia_valid &= ~ATTR_SIZE;
  960. #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
  961. | ATTR_GID | ATTR_UID | ATTR_MODE)
  962. if (!(attr->ia_valid & OCFS2_VALID_ATTRS))
  963. return 0;
  964. status = inode_change_ok(inode, attr);
  965. if (status)
  966. return status;
  967. if (is_quota_modification(inode, attr))
  968. dquot_initialize(inode);
  969. size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
  970. if (size_change) {
  971. status = ocfs2_rw_lock(inode, 1);
  972. if (status < 0) {
  973. mlog_errno(status);
  974. goto bail;
  975. }
  976. }
  977. status = ocfs2_inode_lock(inode, &bh, 1);
  978. if (status < 0) {
  979. if (status != -ENOENT)
  980. mlog_errno(status);
  981. goto bail_unlock_rw;
  982. }
  983. if (size_change && attr->ia_size != i_size_read(inode)) {
  984. status = inode_newsize_ok(inode, attr->ia_size);
  985. if (status)
  986. goto bail_unlock;
  987. inode_dio_wait(inode);
  988. if (i_size_read(inode) > attr->ia_size) {
  989. if (ocfs2_should_order_data(inode)) {
  990. status = ocfs2_begin_ordered_truncate(inode,
  991. attr->ia_size);
  992. if (status)
  993. goto bail_unlock;
  994. }
  995. status = ocfs2_truncate_file(inode, bh, attr->ia_size);
  996. } else
  997. status = ocfs2_extend_file(inode, bh, attr->ia_size);
  998. if (status < 0) {
  999. if (status != -ENOSPC)
  1000. mlog_errno(status);
  1001. status = -ENOSPC;
  1002. goto bail_unlock;
  1003. }
  1004. }
  1005. if ((attr->ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
  1006. (attr->ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) {
  1007. /*
  1008. * Gather pointers to quota structures so that allocation /
  1009. * freeing of quota structures happens here and not inside
  1010. * dquot_transfer() where we have problems with lock ordering
  1011. */
  1012. if (attr->ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid
  1013. && OCFS2_HAS_RO_COMPAT_FEATURE(sb,
  1014. OCFS2_FEATURE_RO_COMPAT_USRQUOTA)) {
  1015. transfer_to[USRQUOTA] = dqget(sb, attr->ia_uid,
  1016. USRQUOTA);
  1017. if (!transfer_to[USRQUOTA]) {
  1018. status = -ESRCH;
  1019. goto bail_unlock;
  1020. }
  1021. }
  1022. if (attr->ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid
  1023. && OCFS2_HAS_RO_COMPAT_FEATURE(sb,
  1024. OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)) {
  1025. transfer_to[GRPQUOTA] = dqget(sb, attr->ia_gid,
  1026. GRPQUOTA);
  1027. if (!transfer_to[GRPQUOTA]) {
  1028. status = -ESRCH;
  1029. goto bail_unlock;
  1030. }
  1031. }
  1032. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS +
  1033. 2 * ocfs2_quota_trans_credits(sb));
  1034. if (IS_ERR(handle)) {
  1035. status = PTR_ERR(handle);
  1036. mlog_errno(status);
  1037. goto bail_unlock;
  1038. }
  1039. status = __dquot_transfer(inode, transfer_to);
  1040. if (status < 0)
  1041. goto bail_commit;
  1042. } else {
  1043. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1044. if (IS_ERR(handle)) {
  1045. status = PTR_ERR(handle);
  1046. mlog_errno(status);
  1047. goto bail_unlock;
  1048. }
  1049. }
  1050. /*
  1051. * This will intentionally not wind up calling truncate_setsize(),
  1052. * since all the work for a size change has been done above.
  1053. * Otherwise, we could get into problems with truncate as
  1054. * ip_alloc_sem is used there to protect against i_size
  1055. * changes.
  1056. *
  1057. * XXX: this means the conditional below can probably be removed.
  1058. */
  1059. if ((attr->ia_valid & ATTR_SIZE) &&
  1060. attr->ia_size != i_size_read(inode)) {
  1061. status = vmtruncate(inode, attr->ia_size);
  1062. if (status) {
  1063. mlog_errno(status);
  1064. goto bail_commit;
  1065. }
  1066. }
  1067. setattr_copy(inode, attr);
  1068. mark_inode_dirty(inode);
  1069. status = ocfs2_mark_inode_dirty(handle, inode, bh);
  1070. if (status < 0)
  1071. mlog_errno(status);
  1072. bail_commit:
  1073. ocfs2_commit_trans(osb, handle);
  1074. bail_unlock:
  1075. ocfs2_inode_unlock(inode, 1);
  1076. bail_unlock_rw:
  1077. if (size_change)
  1078. ocfs2_rw_unlock(inode, 1);
  1079. bail:
  1080. brelse(bh);
  1081. /* Release quota pointers in case we acquired them */
  1082. for (qtype = 0; qtype < MAXQUOTAS; qtype++)
  1083. dqput(transfer_to[qtype]);
  1084. if (!status && attr->ia_valid & ATTR_MODE) {
  1085. status = ocfs2_acl_chmod(inode);
  1086. if (status < 0)
  1087. mlog_errno(status);
  1088. }
  1089. return status;
  1090. }
  1091. int ocfs2_getattr(struct vfsmount *mnt,
  1092. struct dentry *dentry,
  1093. struct kstat *stat)
  1094. {
  1095. struct inode *inode = dentry->d_inode;
  1096. struct super_block *sb = dentry->d_inode->i_sb;
  1097. struct ocfs2_super *osb = sb->s_fs_info;
  1098. int err;
  1099. err = ocfs2_inode_revalidate(dentry);
  1100. if (err) {
  1101. if (err != -ENOENT)
  1102. mlog_errno(err);
  1103. goto bail;
  1104. }
  1105. generic_fillattr(inode, stat);
  1106. /* We set the blksize from the cluster size for performance */
  1107. stat->blksize = osb->s_clustersize;
  1108. bail:
  1109. return err;
  1110. }
  1111. int ocfs2_permission(struct inode *inode, int mask)
  1112. {
  1113. int ret;
  1114. if (mask & MAY_NOT_BLOCK)
  1115. return -ECHILD;
  1116. ret = ocfs2_inode_lock(inode, NULL, 0);
  1117. if (ret) {
  1118. if (ret != -ENOENT)
  1119. mlog_errno(ret);
  1120. goto out;
  1121. }
  1122. ret = generic_permission(inode, mask);
  1123. ocfs2_inode_unlock(inode, 0);
  1124. out:
  1125. return ret;
  1126. }
  1127. static int __ocfs2_write_remove_suid(struct inode *inode,
  1128. struct buffer_head *bh)
  1129. {
  1130. int ret;
  1131. handle_t *handle;
  1132. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1133. struct ocfs2_dinode *di;
  1134. trace_ocfs2_write_remove_suid(
  1135. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1136. inode->i_mode);
  1137. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1138. if (IS_ERR(handle)) {
  1139. ret = PTR_ERR(handle);
  1140. mlog_errno(ret);
  1141. goto out;
  1142. }
  1143. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
  1144. OCFS2_JOURNAL_ACCESS_WRITE);
  1145. if (ret < 0) {
  1146. mlog_errno(ret);
  1147. goto out_trans;
  1148. }
  1149. inode->i_mode &= ~S_ISUID;
  1150. if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
  1151. inode->i_mode &= ~S_ISGID;
  1152. di = (struct ocfs2_dinode *) bh->b_data;
  1153. di->i_mode = cpu_to_le16(inode->i_mode);
  1154. ocfs2_journal_dirty(handle, bh);
  1155. out_trans:
  1156. ocfs2_commit_trans(osb, handle);
  1157. out:
  1158. return ret;
  1159. }
  1160. /*
  1161. * Will look for holes and unwritten extents in the range starting at
  1162. * pos for count bytes (inclusive).
  1163. */
  1164. static int ocfs2_check_range_for_holes(struct inode *inode, loff_t pos,
  1165. size_t count)
  1166. {
  1167. int ret = 0;
  1168. unsigned int extent_flags;
  1169. u32 cpos, clusters, extent_len, phys_cpos;
  1170. struct super_block *sb = inode->i_sb;
  1171. cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
  1172. clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
  1173. while (clusters) {
  1174. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
  1175. &extent_flags);
  1176. if (ret < 0) {
  1177. mlog_errno(ret);
  1178. goto out;
  1179. }
  1180. if (phys_cpos == 0 || (extent_flags & OCFS2_EXT_UNWRITTEN)) {
  1181. ret = 1;
  1182. break;
  1183. }
  1184. if (extent_len > clusters)
  1185. extent_len = clusters;
  1186. clusters -= extent_len;
  1187. cpos += extent_len;
  1188. }
  1189. out:
  1190. return ret;
  1191. }
  1192. static int ocfs2_write_remove_suid(struct inode *inode)
  1193. {
  1194. int ret;
  1195. struct buffer_head *bh = NULL;
  1196. ret = ocfs2_read_inode_block(inode, &bh);
  1197. if (ret < 0) {
  1198. mlog_errno(ret);
  1199. goto out;
  1200. }
  1201. ret = __ocfs2_write_remove_suid(inode, bh);
  1202. out:
  1203. brelse(bh);
  1204. return ret;
  1205. }
  1206. /*
  1207. * Allocate enough extents to cover the region starting at byte offset
  1208. * start for len bytes. Existing extents are skipped, any extents
  1209. * added are marked as "unwritten".
  1210. */
  1211. static int ocfs2_allocate_unwritten_extents(struct inode *inode,
  1212. u64 start, u64 len)
  1213. {
  1214. int ret;
  1215. u32 cpos, phys_cpos, clusters, alloc_size;
  1216. u64 end = start + len;
  1217. struct buffer_head *di_bh = NULL;
  1218. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1219. ret = ocfs2_read_inode_block(inode, &di_bh);
  1220. if (ret) {
  1221. mlog_errno(ret);
  1222. goto out;
  1223. }
  1224. /*
  1225. * Nothing to do if the requested reservation range
  1226. * fits within the inode.
  1227. */
  1228. if (ocfs2_size_fits_inline_data(di_bh, end))
  1229. goto out;
  1230. ret = ocfs2_convert_inline_data_to_extents(inode, di_bh);
  1231. if (ret) {
  1232. mlog_errno(ret);
  1233. goto out;
  1234. }
  1235. }
  1236. /*
  1237. * We consider both start and len to be inclusive.
  1238. */
  1239. cpos = start >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  1240. clusters = ocfs2_clusters_for_bytes(inode->i_sb, start + len);
  1241. clusters -= cpos;
  1242. while (clusters) {
  1243. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos,
  1244. &alloc_size, NULL);
  1245. if (ret) {
  1246. mlog_errno(ret);
  1247. goto out;
  1248. }
  1249. /*
  1250. * Hole or existing extent len can be arbitrary, so
  1251. * cap it to our own allocation request.
  1252. */
  1253. if (alloc_size > clusters)
  1254. alloc_size = clusters;
  1255. if (phys_cpos) {
  1256. /*
  1257. * We already have an allocation at this
  1258. * region so we can safely skip it.
  1259. */
  1260. goto next;
  1261. }
  1262. ret = __ocfs2_extend_allocation(inode, cpos, alloc_size, 1);
  1263. if (ret) {
  1264. if (ret != -ENOSPC)
  1265. mlog_errno(ret);
  1266. goto out;
  1267. }
  1268. next:
  1269. cpos += alloc_size;
  1270. clusters -= alloc_size;
  1271. }
  1272. ret = 0;
  1273. out:
  1274. brelse(di_bh);
  1275. return ret;
  1276. }
  1277. /*
  1278. * Truncate a byte range, avoiding pages within partial clusters. This
  1279. * preserves those pages for the zeroing code to write to.
  1280. */
  1281. static void ocfs2_truncate_cluster_pages(struct inode *inode, u64 byte_start,
  1282. u64 byte_len)
  1283. {
  1284. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1285. loff_t start, end;
  1286. struct address_space *mapping = inode->i_mapping;
  1287. start = (loff_t)ocfs2_align_bytes_to_clusters(inode->i_sb, byte_start);
  1288. end = byte_start + byte_len;
  1289. end = end & ~(osb->s_clustersize - 1);
  1290. if (start < end) {
  1291. unmap_mapping_range(mapping, start, end - start, 0);
  1292. truncate_inode_pages_range(mapping, start, end - 1);
  1293. }
  1294. }
  1295. static int ocfs2_zero_partial_clusters(struct inode *inode,
  1296. u64 start, u64 len)
  1297. {
  1298. int ret = 0;
  1299. u64 tmpend, end = start + len;
  1300. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1301. unsigned int csize = osb->s_clustersize;
  1302. handle_t *handle;
  1303. /*
  1304. * The "start" and "end" values are NOT necessarily part of
  1305. * the range whose allocation is being deleted. Rather, this
  1306. * is what the user passed in with the request. We must zero
  1307. * partial clusters here. There's no need to worry about
  1308. * physical allocation - the zeroing code knows to skip holes.
  1309. */
  1310. trace_ocfs2_zero_partial_clusters(
  1311. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1312. (unsigned long long)start, (unsigned long long)end);
  1313. /*
  1314. * If both edges are on a cluster boundary then there's no
  1315. * zeroing required as the region is part of the allocation to
  1316. * be truncated.
  1317. */
  1318. if ((start & (csize - 1)) == 0 && (end & (csize - 1)) == 0)
  1319. goto out;
  1320. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1321. if (IS_ERR(handle)) {
  1322. ret = PTR_ERR(handle);
  1323. mlog_errno(ret);
  1324. goto out;
  1325. }
  1326. /*
  1327. * We want to get the byte offset of the end of the 1st cluster.
  1328. */
  1329. tmpend = (u64)osb->s_clustersize + (start & ~(osb->s_clustersize - 1));
  1330. if (tmpend > end)
  1331. tmpend = end;
  1332. trace_ocfs2_zero_partial_clusters_range1((unsigned long long)start,
  1333. (unsigned long long)tmpend);
  1334. ret = ocfs2_zero_range_for_truncate(inode, handle, start, tmpend);
  1335. if (ret)
  1336. mlog_errno(ret);
  1337. if (tmpend < end) {
  1338. /*
  1339. * This may make start and end equal, but the zeroing
  1340. * code will skip any work in that case so there's no
  1341. * need to catch it up here.
  1342. */
  1343. start = end & ~(osb->s_clustersize - 1);
  1344. trace_ocfs2_zero_partial_clusters_range2(
  1345. (unsigned long long)start, (unsigned long long)end);
  1346. ret = ocfs2_zero_range_for_truncate(inode, handle, start, end);
  1347. if (ret)
  1348. mlog_errno(ret);
  1349. }
  1350. ocfs2_commit_trans(osb, handle);
  1351. out:
  1352. return ret;
  1353. }
  1354. static int ocfs2_find_rec(struct ocfs2_extent_list *el, u32 pos)
  1355. {
  1356. int i;
  1357. struct ocfs2_extent_rec *rec = NULL;
  1358. for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
  1359. rec = &el->l_recs[i];
  1360. if (le32_to_cpu(rec->e_cpos) < pos)
  1361. break;
  1362. }
  1363. return i;
  1364. }
  1365. /*
  1366. * Helper to calculate the punching pos and length in one run, we handle the
  1367. * following three cases in order:
  1368. *
  1369. * - remove the entire record
  1370. * - remove a partial record
  1371. * - no record needs to be removed (hole-punching completed)
  1372. */
  1373. static void ocfs2_calc_trunc_pos(struct inode *inode,
  1374. struct ocfs2_extent_list *el,
  1375. struct ocfs2_extent_rec *rec,
  1376. u32 trunc_start, u32 *trunc_cpos,
  1377. u32 *trunc_len, u32 *trunc_end,
  1378. u64 *blkno, int *done)
  1379. {
  1380. int ret = 0;
  1381. u32 coff, range;
  1382. range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
  1383. if (le32_to_cpu(rec->e_cpos) >= trunc_start) {
  1384. /*
  1385. * remove an entire extent record.
  1386. */
  1387. *trunc_cpos = le32_to_cpu(rec->e_cpos);
  1388. /*
  1389. * Skip holes if any.
  1390. */
  1391. if (range < *trunc_end)
  1392. *trunc_end = range;
  1393. *trunc_len = *trunc_end - le32_to_cpu(rec->e_cpos);
  1394. *blkno = le64_to_cpu(rec->e_blkno);
  1395. *trunc_end = le32_to_cpu(rec->e_cpos);
  1396. } else if (range > trunc_start) {
  1397. /*
  1398. * remove a partial extent record, which means we're
  1399. * removing the last extent record.
  1400. */
  1401. *trunc_cpos = trunc_start;
  1402. /*
  1403. * skip hole if any.
  1404. */
  1405. if (range < *trunc_end)
  1406. *trunc_end = range;
  1407. *trunc_len = *trunc_end - trunc_start;
  1408. coff = trunc_start - le32_to_cpu(rec->e_cpos);
  1409. *blkno = le64_to_cpu(rec->e_blkno) +
  1410. ocfs2_clusters_to_blocks(inode->i_sb, coff);
  1411. *trunc_end = trunc_start;
  1412. } else {
  1413. /*
  1414. * It may have two following possibilities:
  1415. *
  1416. * - last record has been removed
  1417. * - trunc_start was within a hole
  1418. *
  1419. * both two cases mean the completion of hole punching.
  1420. */
  1421. ret = 1;
  1422. }
  1423. *done = ret;
  1424. }
  1425. static int ocfs2_remove_inode_range(struct inode *inode,
  1426. struct buffer_head *di_bh, u64 byte_start,
  1427. u64 byte_len)
  1428. {
  1429. int ret = 0, flags = 0, done = 0, i;
  1430. u32 trunc_start, trunc_len, trunc_end, trunc_cpos, phys_cpos;
  1431. u32 cluster_in_el;
  1432. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1433. struct ocfs2_cached_dealloc_ctxt dealloc;
  1434. struct address_space *mapping = inode->i_mapping;
  1435. struct ocfs2_extent_tree et;
  1436. struct ocfs2_path *path = NULL;
  1437. struct ocfs2_extent_list *el = NULL;
  1438. struct ocfs2_extent_rec *rec = NULL;
  1439. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1440. u64 blkno, refcount_loc = le64_to_cpu(di->i_refcount_loc);
  1441. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
  1442. ocfs2_init_dealloc_ctxt(&dealloc);
  1443. trace_ocfs2_remove_inode_range(
  1444. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1445. (unsigned long long)byte_start,
  1446. (unsigned long long)byte_len);
  1447. if (byte_len == 0)
  1448. return 0;
  1449. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1450. ret = ocfs2_truncate_inline(inode, di_bh, byte_start,
  1451. byte_start + byte_len, 0);
  1452. if (ret) {
  1453. mlog_errno(ret);
  1454. goto out;
  1455. }
  1456. /*
  1457. * There's no need to get fancy with the page cache
  1458. * truncate of an inline-data inode. We're talking
  1459. * about less than a page here, which will be cached
  1460. * in the dinode buffer anyway.
  1461. */
  1462. unmap_mapping_range(mapping, 0, 0, 0);
  1463. truncate_inode_pages(mapping, 0);
  1464. goto out;
  1465. }
  1466. /*
  1467. * For reflinks, we may need to CoW 2 clusters which might be
  1468. * partially zero'd later, if hole's start and end offset were
  1469. * within one cluster(means is not exactly aligned to clustersize).
  1470. */
  1471. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
  1472. ret = ocfs2_cow_file_pos(inode, di_bh, byte_start);
  1473. if (ret) {
  1474. mlog_errno(ret);
  1475. goto out;
  1476. }
  1477. ret = ocfs2_cow_file_pos(inode, di_bh, byte_start + byte_len);
  1478. if (ret) {
  1479. mlog_errno(ret);
  1480. goto out;
  1481. }
  1482. }
  1483. trunc_start = ocfs2_clusters_for_bytes(osb->sb, byte_start);
  1484. trunc_end = (byte_start + byte_len) >> osb->s_clustersize_bits;
  1485. cluster_in_el = trunc_end;
  1486. ret = ocfs2_zero_partial_clusters(inode, byte_start, byte_len);
  1487. if (ret) {
  1488. mlog_errno(ret);
  1489. goto out;
  1490. }
  1491. path = ocfs2_new_path_from_et(&et);
  1492. if (!path) {
  1493. ret = -ENOMEM;
  1494. mlog_errno(ret);
  1495. goto out;
  1496. }
  1497. while (trunc_end > trunc_start) {
  1498. ret = ocfs2_find_path(INODE_CACHE(inode), path,
  1499. cluster_in_el);
  1500. if (ret) {
  1501. mlog_errno(ret);
  1502. goto out;
  1503. }
  1504. el = path_leaf_el(path);
  1505. i = ocfs2_find_rec(el, trunc_end);
  1506. /*
  1507. * Need to go to previous extent block.
  1508. */
  1509. if (i < 0) {
  1510. if (path->p_tree_depth == 0)
  1511. break;
  1512. ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
  1513. path,
  1514. &cluster_in_el);
  1515. if (ret) {
  1516. mlog_errno(ret);
  1517. goto out;
  1518. }
  1519. /*
  1520. * We've reached the leftmost extent block,
  1521. * it's safe to leave.
  1522. */
  1523. if (cluster_in_el == 0)
  1524. break;
  1525. /*
  1526. * The 'pos' searched for previous extent block is
  1527. * always one cluster less than actual trunc_end.
  1528. */
  1529. trunc_end = cluster_in_el + 1;
  1530. ocfs2_reinit_path(path, 1);
  1531. continue;
  1532. } else
  1533. rec = &el->l_recs[i];
  1534. ocfs2_calc_trunc_pos(inode, el, rec, trunc_start, &trunc_cpos,
  1535. &trunc_len, &trunc_end, &blkno, &done);
  1536. if (done)
  1537. break;
  1538. flags = rec->e_flags;
  1539. phys_cpos = ocfs2_blocks_to_clusters(inode->i_sb, blkno);
  1540. ret = ocfs2_remove_btree_range(inode, &et, trunc_cpos,
  1541. phys_cpos, trunc_len, flags,
  1542. &dealloc, refcount_loc);
  1543. if (ret < 0) {
  1544. mlog_errno(ret);
  1545. goto out;
  1546. }
  1547. cluster_in_el = trunc_end;
  1548. ocfs2_reinit_path(path, 1);
  1549. }
  1550. ocfs2_truncate_cluster_pages(inode, byte_start, byte_len);
  1551. out:
  1552. ocfs2_schedule_truncate_log_flush(osb, 1);
  1553. ocfs2_run_deallocs(osb, &dealloc);
  1554. return ret;
  1555. }
  1556. /*
  1557. * Parts of this function taken from xfs_change_file_space()
  1558. */
  1559. static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
  1560. loff_t f_pos, unsigned int cmd,
  1561. struct ocfs2_space_resv *sr,
  1562. int change_size)
  1563. {
  1564. int ret;
  1565. s64 llen;
  1566. loff_t size;
  1567. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1568. struct buffer_head *di_bh = NULL;
  1569. handle_t *handle;
  1570. unsigned long long max_off = inode->i_sb->s_maxbytes;
  1571. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  1572. return -EROFS;
  1573. mutex_lock(&inode->i_mutex);
  1574. /*
  1575. * This prevents concurrent writes on other nodes
  1576. */
  1577. ret = ocfs2_rw_lock(inode, 1);
  1578. if (ret) {
  1579. mlog_errno(ret);
  1580. goto out;
  1581. }
  1582. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  1583. if (ret) {
  1584. mlog_errno(ret);
  1585. goto out_rw_unlock;
  1586. }
  1587. if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
  1588. ret = -EPERM;
  1589. goto out_inode_unlock;
  1590. }
  1591. switch (sr->l_whence) {
  1592. case 0: /*SEEK_SET*/
  1593. break;
  1594. case 1: /*SEEK_CUR*/
  1595. sr->l_start += f_pos;
  1596. break;
  1597. case 2: /*SEEK_END*/
  1598. sr->l_start += i_size_read(inode);
  1599. break;
  1600. default:
  1601. ret = -EINVAL;
  1602. goto out_inode_unlock;
  1603. }
  1604. sr->l_whence = 0;
  1605. llen = sr->l_len > 0 ? sr->l_len - 1 : sr->l_len;
  1606. if (sr->l_start < 0
  1607. || sr->l_start > max_off
  1608. || (sr->l_start + llen) < 0
  1609. || (sr->l_start + llen) > max_off) {
  1610. ret = -EINVAL;
  1611. goto out_inode_unlock;
  1612. }
  1613. size = sr->l_start + sr->l_len;
  1614. if (cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) {
  1615. if (sr->l_len <= 0) {
  1616. ret = -EINVAL;
  1617. goto out_inode_unlock;
  1618. }
  1619. }
  1620. if (file && should_remove_suid(file->f_path.dentry)) {
  1621. ret = __ocfs2_write_remove_suid(inode, di_bh);
  1622. if (ret) {
  1623. mlog_errno(ret);
  1624. goto out_inode_unlock;
  1625. }
  1626. }
  1627. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  1628. switch (cmd) {
  1629. case OCFS2_IOC_RESVSP:
  1630. case OCFS2_IOC_RESVSP64:
  1631. /*
  1632. * This takes unsigned offsets, but the signed ones we
  1633. * pass have been checked against overflow above.
  1634. */
  1635. ret = ocfs2_allocate_unwritten_extents(inode, sr->l_start,
  1636. sr->l_len);
  1637. break;
  1638. case OCFS2_IOC_UNRESVSP:
  1639. case OCFS2_IOC_UNRESVSP64:
  1640. ret = ocfs2_remove_inode_range(inode, di_bh, sr->l_start,
  1641. sr->l_len);
  1642. break;
  1643. default:
  1644. ret = -EINVAL;
  1645. }
  1646. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  1647. if (ret) {
  1648. mlog_errno(ret);
  1649. goto out_inode_unlock;
  1650. }
  1651. /*
  1652. * We update c/mtime for these changes
  1653. */
  1654. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1655. if (IS_ERR(handle)) {
  1656. ret = PTR_ERR(handle);
  1657. mlog_errno(ret);
  1658. goto out_inode_unlock;
  1659. }
  1660. if (change_size && i_size_read(inode) < size)
  1661. i_size_write(inode, size);
  1662. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  1663. ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
  1664. if (ret < 0)
  1665. mlog_errno(ret);
  1666. if (file->f_flags & O_SYNC)
  1667. handle->h_sync = 1;
  1668. ocfs2_commit_trans(osb, handle);
  1669. out_inode_unlock:
  1670. brelse(di_bh);
  1671. ocfs2_inode_unlock(inode, 1);
  1672. out_rw_unlock:
  1673. ocfs2_rw_unlock(inode, 1);
  1674. out:
  1675. mutex_unlock(&inode->i_mutex);
  1676. return ret;
  1677. }
  1678. int ocfs2_change_file_space(struct file *file, unsigned int cmd,
  1679. struct ocfs2_space_resv *sr)
  1680. {
  1681. struct inode *inode = file->f_path.dentry->d_inode;
  1682. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1683. if ((cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) &&
  1684. !ocfs2_writes_unwritten_extents(osb))
  1685. return -ENOTTY;
  1686. else if ((cmd == OCFS2_IOC_UNRESVSP || cmd == OCFS2_IOC_UNRESVSP64) &&
  1687. !ocfs2_sparse_alloc(osb))
  1688. return -ENOTTY;
  1689. if (!S_ISREG(inode->i_mode))
  1690. return -EINVAL;
  1691. if (!(file->f_mode & FMODE_WRITE))
  1692. return -EBADF;
  1693. return __ocfs2_change_file_space(file, inode, file->f_pos, cmd, sr, 0);
  1694. }
  1695. static long ocfs2_fallocate(struct file *file, int mode, loff_t offset,
  1696. loff_t len)
  1697. {
  1698. struct inode *inode = file->f_path.dentry->d_inode;
  1699. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1700. struct ocfs2_space_resv sr;
  1701. int change_size = 1;
  1702. int cmd = OCFS2_IOC_RESVSP64;
  1703. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  1704. return -EOPNOTSUPP;
  1705. if (!ocfs2_writes_unwritten_extents(osb))
  1706. return -EOPNOTSUPP;
  1707. if (mode & FALLOC_FL_KEEP_SIZE)
  1708. change_size = 0;
  1709. if (mode & FALLOC_FL_PUNCH_HOLE)
  1710. cmd = OCFS2_IOC_UNRESVSP64;
  1711. sr.l_whence = 0;
  1712. sr.l_start = (s64)offset;
  1713. sr.l_len = (s64)len;
  1714. return __ocfs2_change_file_space(NULL, inode, offset, cmd, &sr,
  1715. change_size);
  1716. }
  1717. int ocfs2_check_range_for_refcount(struct inode *inode, loff_t pos,
  1718. size_t count)
  1719. {
  1720. int ret = 0;
  1721. unsigned int extent_flags;
  1722. u32 cpos, clusters, extent_len, phys_cpos;
  1723. struct super_block *sb = inode->i_sb;
  1724. if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb)) ||
  1725. !(OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) ||
  1726. OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  1727. return 0;
  1728. cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
  1729. clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
  1730. while (clusters) {
  1731. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
  1732. &extent_flags);
  1733. if (ret < 0) {
  1734. mlog_errno(ret);
  1735. goto out;
  1736. }
  1737. if (phys_cpos && (extent_flags & OCFS2_EXT_REFCOUNTED)) {
  1738. ret = 1;
  1739. break;
  1740. }
  1741. if (extent_len > clusters)
  1742. extent_len = clusters;
  1743. clusters -= extent_len;
  1744. cpos += extent_len;
  1745. }
  1746. out:
  1747. return ret;
  1748. }
  1749. static void ocfs2_aiodio_wait(struct inode *inode)
  1750. {
  1751. wait_queue_head_t *wq = ocfs2_ioend_wq(inode);
  1752. wait_event(*wq, (atomic_read(&OCFS2_I(inode)->ip_unaligned_aio) == 0));
  1753. }
  1754. static int ocfs2_is_io_unaligned(struct inode *inode, size_t count, loff_t pos)
  1755. {
  1756. int blockmask = inode->i_sb->s_blocksize - 1;
  1757. loff_t final_size = pos + count;
  1758. if ((pos & blockmask) || (final_size & blockmask))
  1759. return 1;
  1760. return 0;
  1761. }
  1762. static int ocfs2_prepare_inode_for_refcount(struct inode *inode,
  1763. struct file *file,
  1764. loff_t pos, size_t count,
  1765. int *meta_level)
  1766. {
  1767. int ret;
  1768. struct buffer_head *di_bh = NULL;
  1769. u32 cpos = pos >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  1770. u32 clusters =
  1771. ocfs2_clusters_for_bytes(inode->i_sb, pos + count) - cpos;
  1772. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  1773. if (ret) {
  1774. mlog_errno(ret);
  1775. goto out;
  1776. }
  1777. *meta_level = 1;
  1778. ret = ocfs2_refcount_cow(inode, file, di_bh, cpos, clusters, UINT_MAX);
  1779. if (ret)
  1780. mlog_errno(ret);
  1781. out:
  1782. brelse(di_bh);
  1783. return ret;
  1784. }
  1785. static int ocfs2_prepare_inode_for_write(struct file *file,
  1786. loff_t *ppos,
  1787. size_t count,
  1788. int appending,
  1789. int *direct_io,
  1790. int *has_refcount)
  1791. {
  1792. int ret = 0, meta_level = 0;
  1793. struct dentry *dentry = file->f_path.dentry;
  1794. struct inode *inode = dentry->d_inode;
  1795. loff_t saved_pos = 0, end;
  1796. /*
  1797. * We start with a read level meta lock and only jump to an ex
  1798. * if we need to make modifications here.
  1799. */
  1800. for(;;) {
  1801. ret = ocfs2_inode_lock(inode, NULL, meta_level);
  1802. if (ret < 0) {
  1803. meta_level = -1;
  1804. mlog_errno(ret);
  1805. goto out;
  1806. }
  1807. /* Clear suid / sgid if necessary. We do this here
  1808. * instead of later in the write path because
  1809. * remove_suid() calls ->setattr without any hint that
  1810. * we may have already done our cluster locking. Since
  1811. * ocfs2_setattr() *must* take cluster locks to
  1812. * proceed, this will lead us to recursively lock the
  1813. * inode. There's also the dinode i_size state which
  1814. * can be lost via setattr during extending writes (we
  1815. * set inode->i_size at the end of a write. */
  1816. if (should_remove_suid(dentry)) {
  1817. if (meta_level == 0) {
  1818. ocfs2_inode_unlock(inode, meta_level);
  1819. meta_level = 1;
  1820. continue;
  1821. }
  1822. ret = ocfs2_write_remove_suid(inode);
  1823. if (ret < 0) {
  1824. mlog_errno(ret);
  1825. goto out_unlock;
  1826. }
  1827. }
  1828. /* work on a copy of ppos until we're sure that we won't have
  1829. * to recalculate it due to relocking. */
  1830. if (appending)
  1831. saved_pos = i_size_read(inode);
  1832. else
  1833. saved_pos = *ppos;
  1834. end = saved_pos + count;
  1835. ret = ocfs2_check_range_for_refcount(inode, saved_pos, count);
  1836. if (ret == 1) {
  1837. ocfs2_inode_unlock(inode, meta_level);
  1838. meta_level = -1;
  1839. ret = ocfs2_prepare_inode_for_refcount(inode,
  1840. file,
  1841. saved_pos,
  1842. count,
  1843. &meta_level);
  1844. if (has_refcount)
  1845. *has_refcount = 1;
  1846. if (direct_io)
  1847. *direct_io = 0;
  1848. }
  1849. if (ret < 0) {
  1850. mlog_errno(ret);
  1851. goto out_unlock;
  1852. }
  1853. /*
  1854. * Skip the O_DIRECT checks if we don't need
  1855. * them.
  1856. */
  1857. if (!direct_io || !(*direct_io))
  1858. break;
  1859. /*
  1860. * There's no sane way to do direct writes to an inode
  1861. * with inline data.
  1862. */
  1863. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1864. *direct_io = 0;
  1865. break;
  1866. }
  1867. /*
  1868. * Allowing concurrent direct writes means
  1869. * i_size changes wouldn't be synchronized, so
  1870. * one node could wind up truncating another
  1871. * nodes writes.
  1872. */
  1873. if (end > i_size_read(inode)) {
  1874. *direct_io = 0;
  1875. break;
  1876. }
  1877. /*
  1878. * We don't fill holes during direct io, so
  1879. * check for them here. If any are found, the
  1880. * caller will have to retake some cluster
  1881. * locks and initiate the io as buffered.
  1882. */
  1883. ret = ocfs2_check_range_for_holes(inode, saved_pos, count);
  1884. if (ret == 1) {
  1885. *direct_io = 0;
  1886. ret = 0;
  1887. } else if (ret < 0)
  1888. mlog_errno(ret);
  1889. break;
  1890. }
  1891. if (appending)
  1892. *ppos = saved_pos;
  1893. out_unlock:
  1894. trace_ocfs2_prepare_inode_for_write(OCFS2_I(inode)->ip_blkno,
  1895. saved_pos, appending, count,
  1896. direct_io, has_refcount);
  1897. if (meta_level >= 0)
  1898. ocfs2_inode_unlock(inode, meta_level);
  1899. out:
  1900. return ret;
  1901. }
  1902. static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
  1903. const struct iovec *iov,
  1904. unsigned long nr_segs,
  1905. loff_t pos)
  1906. {
  1907. int ret, direct_io, appending, rw_level, have_alloc_sem = 0;
  1908. int can_do_direct, has_refcount = 0;
  1909. ssize_t written = 0;
  1910. size_t ocount; /* original count */
  1911. size_t count; /* after file limit checks */
  1912. loff_t old_size, *ppos = &iocb->ki_pos;
  1913. u32 old_clusters;
  1914. struct file *file = iocb->ki_filp;
  1915. struct inode *inode = file->f_path.dentry->d_inode;
  1916. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1917. int full_coherency = !(osb->s_mount_opt &
  1918. OCFS2_MOUNT_COHERENCY_BUFFERED);
  1919. int unaligned_dio = 0;
  1920. trace_ocfs2_file_aio_write(inode, file, file->f_path.dentry,
  1921. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1922. file->f_path.dentry->d_name.len,
  1923. file->f_path.dentry->d_name.name,
  1924. (unsigned int)nr_segs);
  1925. if (iocb->ki_left == 0)
  1926. return 0;
  1927. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1928. appending = file->f_flags & O_APPEND ? 1 : 0;
  1929. direct_io = file->f_flags & O_DIRECT ? 1 : 0;
  1930. mutex_lock(&inode->i_mutex);
  1931. ocfs2_iocb_clear_sem_locked(iocb);
  1932. relock:
  1933. /* to match setattr's i_mutex -> rw_lock ordering */
  1934. if (direct_io) {
  1935. have_alloc_sem = 1;
  1936. /* communicate with ocfs2_dio_end_io */
  1937. ocfs2_iocb_set_sem_locked(iocb);
  1938. }
  1939. /*
  1940. * Concurrent O_DIRECT writes are allowed with
  1941. * mount_option "coherency=buffered".
  1942. */
  1943. rw_level = (!direct_io || full_coherency);
  1944. ret = ocfs2_rw_lock(inode, rw_level);
  1945. if (ret < 0) {
  1946. mlog_errno(ret);
  1947. goto out_sems;
  1948. }
  1949. /*
  1950. * O_DIRECT writes with "coherency=full" need to take EX cluster
  1951. * inode_lock to guarantee coherency.
  1952. */
  1953. if (direct_io && full_coherency) {
  1954. /*
  1955. * We need to take and drop the inode lock to force
  1956. * other nodes to drop their caches. Buffered I/O
  1957. * already does this in write_begin().
  1958. */
  1959. ret = ocfs2_inode_lock(inode, NULL, 1);
  1960. if (ret < 0) {
  1961. mlog_errno(ret);
  1962. goto out_sems;
  1963. }
  1964. ocfs2_inode_unlock(inode, 1);
  1965. }
  1966. can_do_direct = direct_io;
  1967. ret = ocfs2_prepare_inode_for_write(file, ppos,
  1968. iocb->ki_left, appending,
  1969. &can_do_direct, &has_refcount);
  1970. if (ret < 0) {
  1971. mlog_errno(ret);
  1972. goto out;
  1973. }
  1974. if (direct_io && !is_sync_kiocb(iocb))
  1975. unaligned_dio = ocfs2_is_io_unaligned(inode, iocb->ki_left,
  1976. *ppos);
  1977. /*
  1978. * We can't complete the direct I/O as requested, fall back to
  1979. * buffered I/O.
  1980. */
  1981. if (direct_io && !can_do_direct) {
  1982. ocfs2_rw_unlock(inode, rw_level);
  1983. have_alloc_sem = 0;
  1984. rw_level = -1;
  1985. direct_io = 0;
  1986. goto relock;
  1987. }
  1988. if (unaligned_dio) {
  1989. /*
  1990. * Wait on previous unaligned aio to complete before
  1991. * proceeding.
  1992. */
  1993. ocfs2_aiodio_wait(inode);
  1994. /* Mark the iocb as needing a decrement in ocfs2_dio_end_io */
  1995. atomic_inc(&OCFS2_I(inode)->ip_unaligned_aio);
  1996. ocfs2_iocb_set_unaligned_aio(iocb);
  1997. }
  1998. /*
  1999. * To later detect whether a journal commit for sync writes is
  2000. * necessary, we sample i_size, and cluster count here.
  2001. */
  2002. old_size = i_size_read(inode);
  2003. old_clusters = OCFS2_I(inode)->ip_clusters;
  2004. /* communicate with ocfs2_dio_end_io */
  2005. ocfs2_iocb_set_rw_locked(iocb, rw_level);
  2006. ret = generic_segment_checks(iov, &nr_segs, &ocount,
  2007. VERIFY_READ);
  2008. if (ret)
  2009. goto out_dio;
  2010. count = ocount;
  2011. ret = generic_write_checks(file, ppos, &count,
  2012. S_ISBLK(inode->i_mode));
  2013. if (ret)
  2014. goto out_dio;
  2015. if (direct_io) {
  2016. written = generic_file_direct_write(iocb, iov, &nr_segs, *ppos,
  2017. ppos, count, ocount);
  2018. if (written < 0) {
  2019. ret = written;
  2020. goto out_dio;
  2021. }
  2022. } else {
  2023. current->backing_dev_info = file->f_mapping->backing_dev_info;
  2024. written = generic_file_buffered_write(iocb, iov, nr_segs, *ppos,
  2025. ppos, count, 0);
  2026. current->backing_dev_info = NULL;
  2027. }
  2028. out_dio:
  2029. /* buffered aio wouldn't have proper lock coverage today */
  2030. BUG_ON(ret == -EIOCBQUEUED && !(file->f_flags & O_DIRECT));
  2031. if (((file->f_flags & O_DSYNC) && !direct_io) || IS_SYNC(inode) ||
  2032. ((file->f_flags & O_DIRECT) && !direct_io)) {
  2033. ret = filemap_fdatawrite_range(file->f_mapping, pos,
  2034. pos + count - 1);
  2035. if (ret < 0)
  2036. written = ret;
  2037. if (!ret && ((old_size != i_size_read(inode)) ||
  2038. (old_clusters != OCFS2_I(inode)->ip_clusters) ||
  2039. has_refcount)) {
  2040. ret = jbd2_journal_force_commit(osb->journal->j_journal);
  2041. if (ret < 0)
  2042. written = ret;
  2043. }
  2044. if (!ret)
  2045. ret = filemap_fdatawait_range(file->f_mapping, pos,
  2046. pos + count - 1);
  2047. }
  2048. /*
  2049. * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
  2050. * function pointer which is called when o_direct io completes so that
  2051. * it can unlock our rw lock.
  2052. * Unfortunately there are error cases which call end_io and others
  2053. * that don't. so we don't have to unlock the rw_lock if either an
  2054. * async dio is going to do it in the future or an end_io after an
  2055. * error has already done it.
  2056. */
  2057. if ((ret == -EIOCBQUEUED) || (!ocfs2_iocb_is_rw_locked(iocb))) {
  2058. rw_level = -1;
  2059. have_alloc_sem = 0;
  2060. unaligned_dio = 0;
  2061. }
  2062. if (unaligned_dio)
  2063. atomic_dec(&OCFS2_I(inode)->ip_unaligned_aio);
  2064. out:
  2065. if (rw_level != -1)
  2066. ocfs2_rw_unlock(inode, rw_level);
  2067. out_sems:
  2068. if (have_alloc_sem)
  2069. ocfs2_iocb_clear_sem_locked(iocb);
  2070. mutex_unlock(&inode->i_mutex);
  2071. if (written)
  2072. ret = written;
  2073. return ret;
  2074. }
  2075. static int ocfs2_splice_to_file(struct pipe_inode_info *pipe,
  2076. struct file *out,
  2077. struct splice_desc *sd)
  2078. {
  2079. int ret;
  2080. ret = ocfs2_prepare_inode_for_write(out, &sd->pos,
  2081. sd->total_len, 0, NULL, NULL);
  2082. if (ret < 0) {
  2083. mlog_errno(ret);
  2084. return ret;
  2085. }
  2086. return splice_from_pipe_feed(pipe, sd, pipe_to_file);
  2087. }
  2088. static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe,
  2089. struct file *out,
  2090. loff_t *ppos,
  2091. size_t len,
  2092. unsigned int flags)
  2093. {
  2094. int ret;
  2095. struct address_space *mapping = out->f_mapping;
  2096. struct inode *inode = mapping->host;
  2097. struct splice_desc sd = {
  2098. .total_len = len,
  2099. .flags = flags,
  2100. .pos = *ppos,
  2101. .u.file = out,
  2102. };
  2103. trace_ocfs2_file_splice_write(inode, out, out->f_path.dentry,
  2104. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  2105. out->f_path.dentry->d_name.len,
  2106. out->f_path.dentry->d_name.name, len);
  2107. if (pipe->inode)
  2108. mutex_lock_nested(&pipe->inode->i_mutex, I_MUTEX_PARENT);
  2109. splice_from_pipe_begin(&sd);
  2110. do {
  2111. ret = splice_from_pipe_next(pipe, &sd);
  2112. if (ret <= 0)
  2113. break;
  2114. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  2115. ret = ocfs2_rw_lock(inode, 1);
  2116. if (ret < 0)
  2117. mlog_errno(ret);
  2118. else {
  2119. ret = ocfs2_splice_to_file(pipe, out, &sd);
  2120. ocfs2_rw_unlock(inode, 1);
  2121. }
  2122. mutex_unlock(&inode->i_mutex);
  2123. } while (ret > 0);
  2124. splice_from_pipe_end(pipe, &sd);
  2125. if (pipe->inode)
  2126. mutex_unlock(&pipe->inode->i_mutex);
  2127. if (sd.num_spliced)
  2128. ret = sd.num_spliced;
  2129. if (ret > 0) {
  2130. unsigned long nr_pages;
  2131. int err;
  2132. nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  2133. err = generic_write_sync(out, *ppos, ret);
  2134. if (err)
  2135. ret = err;
  2136. else
  2137. *ppos += ret;
  2138. balance_dirty_pages_ratelimited_nr(mapping, nr_pages);
  2139. }
  2140. return ret;
  2141. }
  2142. static ssize_t ocfs2_file_splice_read(struct file *in,
  2143. loff_t *ppos,
  2144. struct pipe_inode_info *pipe,
  2145. size_t len,
  2146. unsigned int flags)
  2147. {
  2148. int ret = 0, lock_level = 0;
  2149. struct inode *inode = in->f_path.dentry->d_inode;
  2150. trace_ocfs2_file_splice_read(inode, in, in->f_path.dentry,
  2151. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  2152. in->f_path.dentry->d_name.len,
  2153. in->f_path.dentry->d_name.name, len);
  2154. /*
  2155. * See the comment in ocfs2_file_aio_read()
  2156. */
  2157. ret = ocfs2_inode_lock_atime(inode, in->f_vfsmnt, &lock_level);
  2158. if (ret < 0) {
  2159. mlog_errno(ret);
  2160. goto bail;
  2161. }
  2162. ocfs2_inode_unlock(inode, lock_level);
  2163. ret = generic_file_splice_read(in, ppos, pipe, len, flags);
  2164. bail:
  2165. return ret;
  2166. }
  2167. static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
  2168. const struct iovec *iov,
  2169. unsigned long nr_segs,
  2170. loff_t pos)
  2171. {
  2172. int ret = 0, rw_level = -1, have_alloc_sem = 0, lock_level = 0;
  2173. struct file *filp = iocb->ki_filp;
  2174. struct inode *inode = filp->f_path.dentry->d_inode;
  2175. trace_ocfs2_file_aio_read(inode, filp, filp->f_path.dentry,
  2176. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  2177. filp->f_path.dentry->d_name.len,
  2178. filp->f_path.dentry->d_name.name, nr_segs);
  2179. if (!inode) {
  2180. ret = -EINVAL;
  2181. mlog_errno(ret);
  2182. goto bail;
  2183. }
  2184. ocfs2_iocb_clear_sem_locked(iocb);
  2185. /*
  2186. * buffered reads protect themselves in ->readpage(). O_DIRECT reads
  2187. * need locks to protect pending reads from racing with truncate.
  2188. */
  2189. if (filp->f_flags & O_DIRECT) {
  2190. have_alloc_sem = 1;
  2191. ocfs2_iocb_set_sem_locked(iocb);
  2192. ret = ocfs2_rw_lock(inode, 0);
  2193. if (ret < 0) {
  2194. mlog_errno(ret);
  2195. goto bail;
  2196. }
  2197. rw_level = 0;
  2198. /* communicate with ocfs2_dio_end_io */
  2199. ocfs2_iocb_set_rw_locked(iocb, rw_level);
  2200. }
  2201. /*
  2202. * We're fine letting folks race truncates and extending
  2203. * writes with read across the cluster, just like they can
  2204. * locally. Hence no rw_lock during read.
  2205. *
  2206. * Take and drop the meta data lock to update inode fields
  2207. * like i_size. This allows the checks down below
  2208. * generic_file_aio_read() a chance of actually working.
  2209. */
  2210. ret = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
  2211. if (ret < 0) {
  2212. mlog_errno(ret);
  2213. goto bail;
  2214. }
  2215. ocfs2_inode_unlock(inode, lock_level);
  2216. ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos);
  2217. trace_generic_file_aio_read_ret(ret);
  2218. /* buffered aio wouldn't have proper lock coverage today */
  2219. BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
  2220. /* see ocfs2_file_aio_write */
  2221. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  2222. rw_level = -1;
  2223. have_alloc_sem = 0;
  2224. }
  2225. bail:
  2226. if (have_alloc_sem)
  2227. ocfs2_iocb_clear_sem_locked(iocb);
  2228. if (rw_level != -1)
  2229. ocfs2_rw_unlock(inode, rw_level);
  2230. return ret;
  2231. }
  2232. /* Refer generic_file_llseek_unlocked() */
  2233. static loff_t ocfs2_file_llseek(struct file *file, loff_t offset, int origin)
  2234. {
  2235. struct inode *inode = file->f_mapping->host;
  2236. int ret = 0;
  2237. mutex_lock(&inode->i_mutex);
  2238. switch (origin) {
  2239. case SEEK_SET:
  2240. break;
  2241. case SEEK_END:
  2242. offset += inode->i_size;
  2243. break;
  2244. case SEEK_CUR:
  2245. if (offset == 0) {
  2246. offset = file->f_pos;
  2247. goto out;
  2248. }
  2249. offset += file->f_pos;
  2250. break;
  2251. case SEEK_DATA:
  2252. case SEEK_HOLE:
  2253. ret = ocfs2_seek_data_hole_offset(file, &offset, origin);
  2254. if (ret)
  2255. goto out;
  2256. break;
  2257. default:
  2258. ret = -EINVAL;
  2259. goto out;
  2260. }
  2261. if (offset < 0 && !(file->f_mode & FMODE_UNSIGNED_OFFSET))
  2262. ret = -EINVAL;
  2263. if (!ret && offset > inode->i_sb->s_maxbytes)
  2264. ret = -EINVAL;
  2265. if (ret)
  2266. goto out;
  2267. if (offset != file->f_pos) {
  2268. file->f_pos = offset;
  2269. file->f_version = 0;
  2270. }
  2271. out:
  2272. mutex_unlock(&inode->i_mutex);
  2273. if (ret)
  2274. return ret;
  2275. return offset;
  2276. }
  2277. const struct inode_operations ocfs2_file_iops = {
  2278. .setattr = ocfs2_setattr,
  2279. .getattr = ocfs2_getattr,
  2280. .permission = ocfs2_permission,
  2281. .setxattr = generic_setxattr,
  2282. .getxattr = generic_getxattr,
  2283. .listxattr = ocfs2_listxattr,
  2284. .removexattr = generic_removexattr,
  2285. .fiemap = ocfs2_fiemap,
  2286. .get_acl = ocfs2_iop_get_acl,
  2287. };
  2288. const struct inode_operations ocfs2_special_file_iops = {
  2289. .setattr = ocfs2_setattr,
  2290. .getattr = ocfs2_getattr,
  2291. .permission = ocfs2_permission,
  2292. .get_acl = ocfs2_iop_get_acl,
  2293. };
  2294. /*
  2295. * Other than ->lock, keep ocfs2_fops and ocfs2_dops in sync with
  2296. * ocfs2_fops_no_plocks and ocfs2_dops_no_plocks!
  2297. */
  2298. const struct file_operations ocfs2_fops = {
  2299. .llseek = ocfs2_file_llseek,
  2300. .read = do_sync_read,
  2301. .write = do_sync_write,
  2302. .mmap = ocfs2_mmap,
  2303. .fsync = ocfs2_sync_file,
  2304. .release = ocfs2_file_release,
  2305. .open = ocfs2_file_open,
  2306. .aio_read = ocfs2_file_aio_read,
  2307. .aio_write = ocfs2_file_aio_write,
  2308. .unlocked_ioctl = ocfs2_ioctl,
  2309. #ifdef CONFIG_COMPAT
  2310. .compat_ioctl = ocfs2_compat_ioctl,
  2311. #endif
  2312. .lock = ocfs2_lock,
  2313. .flock = ocfs2_flock,
  2314. .splice_read = ocfs2_file_splice_read,
  2315. .splice_write = ocfs2_file_splice_write,
  2316. .fallocate = ocfs2_fallocate,
  2317. };
  2318. const struct file_operations ocfs2_dops = {
  2319. .llseek = generic_file_llseek,
  2320. .read = generic_read_dir,
  2321. .readdir = ocfs2_readdir,
  2322. .fsync = ocfs2_sync_file,
  2323. .release = ocfs2_dir_release,
  2324. .open = ocfs2_dir_open,
  2325. .unlocked_ioctl = ocfs2_ioctl,
  2326. #ifdef CONFIG_COMPAT
  2327. .compat_ioctl = ocfs2_compat_ioctl,
  2328. #endif
  2329. .lock = ocfs2_lock,
  2330. .flock = ocfs2_flock,
  2331. };
  2332. /*
  2333. * POSIX-lockless variants of our file_operations.
  2334. *
  2335. * These will be used if the underlying cluster stack does not support
  2336. * posix file locking, if the user passes the "localflocks" mount
  2337. * option, or if we have a local-only fs.
  2338. *
  2339. * ocfs2_flock is in here because all stacks handle UNIX file locks,
  2340. * so we still want it in the case of no stack support for
  2341. * plocks. Internally, it will do the right thing when asked to ignore
  2342. * the cluster.
  2343. */
  2344. const struct file_operations ocfs2_fops_no_plocks = {
  2345. .llseek = ocfs2_file_llseek,
  2346. .read = do_sync_read,
  2347. .write = do_sync_write,
  2348. .mmap = ocfs2_mmap,
  2349. .fsync = ocfs2_sync_file,
  2350. .release = ocfs2_file_release,
  2351. .open = ocfs2_file_open,
  2352. .aio_read = ocfs2_file_aio_read,
  2353. .aio_write = ocfs2_file_aio_write,
  2354. .unlocked_ioctl = ocfs2_ioctl,
  2355. #ifdef CONFIG_COMPAT
  2356. .compat_ioctl = ocfs2_compat_ioctl,
  2357. #endif
  2358. .flock = ocfs2_flock,
  2359. .splice_read = ocfs2_file_splice_read,
  2360. .splice_write = ocfs2_file_splice_write,
  2361. .fallocate = ocfs2_fallocate,
  2362. };
  2363. const struct file_operations ocfs2_dops_no_plocks = {
  2364. .llseek = generic_file_llseek,
  2365. .read = generic_read_dir,
  2366. .readdir = ocfs2_readdir,
  2367. .fsync = ocfs2_sync_file,
  2368. .release = ocfs2_dir_release,
  2369. .open = ocfs2_dir_open,
  2370. .unlocked_ioctl = ocfs2_ioctl,
  2371. #ifdef CONFIG_COMPAT
  2372. .compat_ioctl = ocfs2_compat_ioctl,
  2373. #endif
  2374. .flock = ocfs2_flock,
  2375. };