inode.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315
  1. /*
  2. * inode.c
  3. *
  4. * PURPOSE
  5. * Inode handling routines for the OSTA-UDF(tm) filesystem.
  6. *
  7. * COPYRIGHT
  8. * This file is distributed under the terms of the GNU General Public
  9. * License (GPL). Copies of the GPL can be obtained from:
  10. * ftp://prep.ai.mit.edu/pub/gnu/GPL
  11. * Each contributing author retains all rights to their own work.
  12. *
  13. * (C) 1998 Dave Boynton
  14. * (C) 1998-2004 Ben Fennema
  15. * (C) 1999-2000 Stelias Computing Inc
  16. *
  17. * HISTORY
  18. *
  19. * 10/04/98 dgb Added rudimentary directory functions
  20. * 10/07/98 Fully working udf_block_map! It works!
  21. * 11/25/98 bmap altered to better support extents
  22. * 12/06/98 blf partition support in udf_iget, udf_block_map
  23. * and udf_read_inode
  24. * 12/12/98 rewrote udf_block_map to handle next extents and descs across
  25. * block boundaries (which is not actually allowed)
  26. * 12/20/98 added support for strategy 4096
  27. * 03/07/99 rewrote udf_block_map (again)
  28. * New funcs, inode_bmap, udf_next_aext
  29. * 04/19/99 Support for writing device EA's for major/minor #
  30. */
  31. #include "udfdecl.h"
  32. #include <linux/mm.h>
  33. #include <linux/module.h>
  34. #include <linux/pagemap.h>
  35. #include <linux/writeback.h>
  36. #include <linux/slab.h>
  37. #include <linux/crc-itu-t.h>
  38. #include <linux/mpage.h>
  39. #include <linux/uio.h>
  40. #include <linux/bio.h>
  41. #include "udf_i.h"
  42. #include "udf_sb.h"
  43. #define EXTENT_MERGE_SIZE 5
  44. static umode_t udf_convert_permissions(struct fileEntry *);
  45. static int udf_update_inode(struct inode *, int);
  46. static int udf_sync_inode(struct inode *inode);
  47. static int udf_alloc_i_data(struct inode *inode, size_t size);
  48. static sector_t inode_getblk(struct inode *, sector_t, int *, int *);
  49. static int8_t udf_insert_aext(struct inode *, struct extent_position,
  50. struct kernel_lb_addr, uint32_t);
  51. static void udf_split_extents(struct inode *, int *, int, udf_pblk_t,
  52. struct kernel_long_ad *, int *);
  53. static void udf_prealloc_extents(struct inode *, int, int,
  54. struct kernel_long_ad *, int *);
  55. static void udf_merge_extents(struct inode *, struct kernel_long_ad *, int *);
  56. static void udf_update_extents(struct inode *, struct kernel_long_ad *, int,
  57. int, struct extent_position *);
  58. static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
  59. static void __udf_clear_extent_cache(struct inode *inode)
  60. {
  61. struct udf_inode_info *iinfo = UDF_I(inode);
  62. if (iinfo->cached_extent.lstart != -1) {
  63. brelse(iinfo->cached_extent.epos.bh);
  64. iinfo->cached_extent.lstart = -1;
  65. }
  66. }
  67. /* Invalidate extent cache */
  68. static void udf_clear_extent_cache(struct inode *inode)
  69. {
  70. struct udf_inode_info *iinfo = UDF_I(inode);
  71. spin_lock(&iinfo->i_extent_cache_lock);
  72. __udf_clear_extent_cache(inode);
  73. spin_unlock(&iinfo->i_extent_cache_lock);
  74. }
  75. /* Return contents of extent cache */
  76. static int udf_read_extent_cache(struct inode *inode, loff_t bcount,
  77. loff_t *lbcount, struct extent_position *pos)
  78. {
  79. struct udf_inode_info *iinfo = UDF_I(inode);
  80. int ret = 0;
  81. spin_lock(&iinfo->i_extent_cache_lock);
  82. if ((iinfo->cached_extent.lstart <= bcount) &&
  83. (iinfo->cached_extent.lstart != -1)) {
  84. /* Cache hit */
  85. *lbcount = iinfo->cached_extent.lstart;
  86. memcpy(pos, &iinfo->cached_extent.epos,
  87. sizeof(struct extent_position));
  88. if (pos->bh)
  89. get_bh(pos->bh);
  90. ret = 1;
  91. }
  92. spin_unlock(&iinfo->i_extent_cache_lock);
  93. return ret;
  94. }
  95. /* Add extent to extent cache */
  96. static void udf_update_extent_cache(struct inode *inode, loff_t estart,
  97. struct extent_position *pos)
  98. {
  99. struct udf_inode_info *iinfo = UDF_I(inode);
  100. spin_lock(&iinfo->i_extent_cache_lock);
  101. /* Invalidate previously cached extent */
  102. __udf_clear_extent_cache(inode);
  103. if (pos->bh)
  104. get_bh(pos->bh);
  105. memcpy(&iinfo->cached_extent.epos, pos, sizeof(*pos));
  106. iinfo->cached_extent.lstart = estart;
  107. switch (iinfo->i_alloc_type) {
  108. case ICBTAG_FLAG_AD_SHORT:
  109. iinfo->cached_extent.epos.offset -= sizeof(struct short_ad);
  110. break;
  111. case ICBTAG_FLAG_AD_LONG:
  112. iinfo->cached_extent.epos.offset -= sizeof(struct long_ad);
  113. break;
  114. }
  115. spin_unlock(&iinfo->i_extent_cache_lock);
  116. }
  117. void udf_evict_inode(struct inode *inode)
  118. {
  119. struct udf_inode_info *iinfo = UDF_I(inode);
  120. int want_delete = 0;
  121. if (!inode->i_nlink && !is_bad_inode(inode)) {
  122. want_delete = 1;
  123. udf_setsize(inode, 0);
  124. udf_update_inode(inode, IS_SYNC(inode));
  125. }
  126. truncate_inode_pages_final(&inode->i_data);
  127. invalidate_inode_buffers(inode);
  128. clear_inode(inode);
  129. if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
  130. inode->i_size != iinfo->i_lenExtents) {
  131. udf_warn(inode->i_sb, "Inode %lu (mode %o) has inode size %llu different from extent length %llu. Filesystem need not be standards compliant.\n",
  132. inode->i_ino, inode->i_mode,
  133. (unsigned long long)inode->i_size,
  134. (unsigned long long)iinfo->i_lenExtents);
  135. }
  136. kfree(iinfo->i_ext.i_data);
  137. iinfo->i_ext.i_data = NULL;
  138. udf_clear_extent_cache(inode);
  139. if (want_delete) {
  140. udf_free_inode(inode);
  141. }
  142. }
  143. static void udf_write_failed(struct address_space *mapping, loff_t to)
  144. {
  145. struct inode *inode = mapping->host;
  146. struct udf_inode_info *iinfo = UDF_I(inode);
  147. loff_t isize = inode->i_size;
  148. if (to > isize) {
  149. truncate_pagecache(inode, isize);
  150. if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
  151. down_write(&iinfo->i_data_sem);
  152. udf_clear_extent_cache(inode);
  153. udf_truncate_extents(inode);
  154. up_write(&iinfo->i_data_sem);
  155. }
  156. }
  157. }
  158. static int udf_writepage(struct page *page, struct writeback_control *wbc)
  159. {
  160. return block_write_full_page(page, udf_get_block, wbc);
  161. }
  162. static int udf_writepages(struct address_space *mapping,
  163. struct writeback_control *wbc)
  164. {
  165. return mpage_writepages(mapping, wbc, udf_get_block);
  166. }
  167. static int udf_readpage(struct file *file, struct page *page)
  168. {
  169. return mpage_readpage(page, udf_get_block);
  170. }
  171. static int udf_readpages(struct file *file, struct address_space *mapping,
  172. struct list_head *pages, unsigned nr_pages)
  173. {
  174. return mpage_readpages(mapping, pages, nr_pages, udf_get_block);
  175. }
  176. static int udf_write_begin(struct file *file, struct address_space *mapping,
  177. loff_t pos, unsigned len, unsigned flags,
  178. struct page **pagep, void **fsdata)
  179. {
  180. int ret;
  181. ret = block_write_begin(mapping, pos, len, flags, pagep, udf_get_block);
  182. if (unlikely(ret))
  183. udf_write_failed(mapping, pos + len);
  184. return ret;
  185. }
  186. static ssize_t udf_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  187. {
  188. struct file *file = iocb->ki_filp;
  189. struct address_space *mapping = file->f_mapping;
  190. struct inode *inode = mapping->host;
  191. size_t count = iov_iter_count(iter);
  192. ssize_t ret;
  193. ret = blockdev_direct_IO(iocb, inode, iter, udf_get_block);
  194. if (unlikely(ret < 0 && iov_iter_rw(iter) == WRITE))
  195. udf_write_failed(mapping, iocb->ki_pos + count);
  196. return ret;
  197. }
  198. static sector_t udf_bmap(struct address_space *mapping, sector_t block)
  199. {
  200. return generic_block_bmap(mapping, block, udf_get_block);
  201. }
  202. const struct address_space_operations udf_aops = {
  203. .readpage = udf_readpage,
  204. .readpages = udf_readpages,
  205. .writepage = udf_writepage,
  206. .writepages = udf_writepages,
  207. .write_begin = udf_write_begin,
  208. .write_end = generic_write_end,
  209. .direct_IO = udf_direct_IO,
  210. .bmap = udf_bmap,
  211. };
  212. /*
  213. * Expand file stored in ICB to a normal one-block-file
  214. *
  215. * This function requires i_data_sem for writing and releases it.
  216. * This function requires i_mutex held
  217. */
  218. int udf_expand_file_adinicb(struct inode *inode)
  219. {
  220. struct page *page;
  221. char *kaddr;
  222. struct udf_inode_info *iinfo = UDF_I(inode);
  223. int err;
  224. struct writeback_control udf_wbc = {
  225. .sync_mode = WB_SYNC_NONE,
  226. .nr_to_write = 1,
  227. };
  228. WARN_ON_ONCE(!inode_is_locked(inode));
  229. if (!iinfo->i_lenAlloc) {
  230. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
  231. iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
  232. else
  233. iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
  234. /* from now on we have normal address_space methods */
  235. inode->i_data.a_ops = &udf_aops;
  236. up_write(&iinfo->i_data_sem);
  237. mark_inode_dirty(inode);
  238. return 0;
  239. }
  240. /*
  241. * Release i_data_sem so that we can lock a page - page lock ranks
  242. * above i_data_sem. i_mutex still protects us against file changes.
  243. */
  244. up_write(&iinfo->i_data_sem);
  245. page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
  246. if (!page)
  247. return -ENOMEM;
  248. if (!PageUptodate(page)) {
  249. kaddr = kmap_atomic(page);
  250. memset(kaddr + iinfo->i_lenAlloc, 0x00,
  251. PAGE_SIZE - iinfo->i_lenAlloc);
  252. memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
  253. iinfo->i_lenAlloc);
  254. flush_dcache_page(page);
  255. SetPageUptodate(page);
  256. kunmap_atomic(kaddr);
  257. }
  258. down_write(&iinfo->i_data_sem);
  259. memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
  260. iinfo->i_lenAlloc);
  261. iinfo->i_lenAlloc = 0;
  262. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
  263. iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
  264. else
  265. iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
  266. /* from now on we have normal address_space methods */
  267. inode->i_data.a_ops = &udf_aops;
  268. up_write(&iinfo->i_data_sem);
  269. err = inode->i_data.a_ops->writepage(page, &udf_wbc);
  270. if (err) {
  271. /* Restore everything back so that we don't lose data... */
  272. lock_page(page);
  273. down_write(&iinfo->i_data_sem);
  274. kaddr = kmap_atomic(page);
  275. memcpy(iinfo->i_ext.i_data + iinfo->i_lenEAttr, kaddr,
  276. inode->i_size);
  277. kunmap_atomic(kaddr);
  278. unlock_page(page);
  279. iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
  280. inode->i_data.a_ops = &udf_adinicb_aops;
  281. up_write(&iinfo->i_data_sem);
  282. }
  283. put_page(page);
  284. mark_inode_dirty(inode);
  285. return err;
  286. }
  287. struct buffer_head *udf_expand_dir_adinicb(struct inode *inode,
  288. udf_pblk_t *block, int *err)
  289. {
  290. udf_pblk_t newblock;
  291. struct buffer_head *dbh = NULL;
  292. struct kernel_lb_addr eloc;
  293. uint8_t alloctype;
  294. struct extent_position epos;
  295. struct udf_fileident_bh sfibh, dfibh;
  296. loff_t f_pos = udf_ext0_offset(inode);
  297. int size = udf_ext0_offset(inode) + inode->i_size;
  298. struct fileIdentDesc cfi, *sfi, *dfi;
  299. struct udf_inode_info *iinfo = UDF_I(inode);
  300. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
  301. alloctype = ICBTAG_FLAG_AD_SHORT;
  302. else
  303. alloctype = ICBTAG_FLAG_AD_LONG;
  304. if (!inode->i_size) {
  305. iinfo->i_alloc_type = alloctype;
  306. mark_inode_dirty(inode);
  307. return NULL;
  308. }
  309. /* alloc block, and copy data to it */
  310. *block = udf_new_block(inode->i_sb, inode,
  311. iinfo->i_location.partitionReferenceNum,
  312. iinfo->i_location.logicalBlockNum, err);
  313. if (!(*block))
  314. return NULL;
  315. newblock = udf_get_pblock(inode->i_sb, *block,
  316. iinfo->i_location.partitionReferenceNum,
  317. 0);
  318. if (!newblock)
  319. return NULL;
  320. dbh = udf_tgetblk(inode->i_sb, newblock);
  321. if (!dbh)
  322. return NULL;
  323. lock_buffer(dbh);
  324. memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
  325. set_buffer_uptodate(dbh);
  326. unlock_buffer(dbh);
  327. mark_buffer_dirty_inode(dbh, inode);
  328. sfibh.soffset = sfibh.eoffset =
  329. f_pos & (inode->i_sb->s_blocksize - 1);
  330. sfibh.sbh = sfibh.ebh = NULL;
  331. dfibh.soffset = dfibh.eoffset = 0;
  332. dfibh.sbh = dfibh.ebh = dbh;
  333. while (f_pos < size) {
  334. iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
  335. sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
  336. NULL, NULL, NULL);
  337. if (!sfi) {
  338. brelse(dbh);
  339. return NULL;
  340. }
  341. iinfo->i_alloc_type = alloctype;
  342. sfi->descTag.tagLocation = cpu_to_le32(*block);
  343. dfibh.soffset = dfibh.eoffset;
  344. dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
  345. dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
  346. if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
  347. sfi->fileIdent +
  348. le16_to_cpu(sfi->lengthOfImpUse))) {
  349. iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
  350. brelse(dbh);
  351. return NULL;
  352. }
  353. }
  354. mark_buffer_dirty_inode(dbh, inode);
  355. memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
  356. iinfo->i_lenAlloc);
  357. iinfo->i_lenAlloc = 0;
  358. eloc.logicalBlockNum = *block;
  359. eloc.partitionReferenceNum =
  360. iinfo->i_location.partitionReferenceNum;
  361. iinfo->i_lenExtents = inode->i_size;
  362. epos.bh = NULL;
  363. epos.block = iinfo->i_location;
  364. epos.offset = udf_file_entry_alloc_offset(inode);
  365. udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
  366. /* UniqueID stuff */
  367. brelse(epos.bh);
  368. mark_inode_dirty(inode);
  369. return dbh;
  370. }
  371. static int udf_get_block(struct inode *inode, sector_t block,
  372. struct buffer_head *bh_result, int create)
  373. {
  374. int err, new;
  375. sector_t phys = 0;
  376. struct udf_inode_info *iinfo;
  377. if (!create) {
  378. phys = udf_block_map(inode, block);
  379. if (phys)
  380. map_bh(bh_result, inode->i_sb, phys);
  381. return 0;
  382. }
  383. err = -EIO;
  384. new = 0;
  385. iinfo = UDF_I(inode);
  386. down_write(&iinfo->i_data_sem);
  387. if (block == iinfo->i_next_alloc_block + 1) {
  388. iinfo->i_next_alloc_block++;
  389. iinfo->i_next_alloc_goal++;
  390. }
  391. udf_clear_extent_cache(inode);
  392. phys = inode_getblk(inode, block, &err, &new);
  393. if (!phys)
  394. goto abort;
  395. if (new)
  396. set_buffer_new(bh_result);
  397. map_bh(bh_result, inode->i_sb, phys);
  398. abort:
  399. up_write(&iinfo->i_data_sem);
  400. return err;
  401. }
  402. static struct buffer_head *udf_getblk(struct inode *inode, udf_pblk_t block,
  403. int create, int *err)
  404. {
  405. struct buffer_head *bh;
  406. struct buffer_head dummy;
  407. dummy.b_state = 0;
  408. dummy.b_blocknr = -1000;
  409. *err = udf_get_block(inode, block, &dummy, create);
  410. if (!*err && buffer_mapped(&dummy)) {
  411. bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
  412. if (buffer_new(&dummy)) {
  413. lock_buffer(bh);
  414. memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
  415. set_buffer_uptodate(bh);
  416. unlock_buffer(bh);
  417. mark_buffer_dirty_inode(bh, inode);
  418. }
  419. return bh;
  420. }
  421. return NULL;
  422. }
  423. /* Extend the file by 'blocks' blocks, return the number of extents added */
  424. static int udf_do_extend_file(struct inode *inode,
  425. struct extent_position *last_pos,
  426. struct kernel_long_ad *last_ext,
  427. sector_t blocks)
  428. {
  429. sector_t add;
  430. int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
  431. struct super_block *sb = inode->i_sb;
  432. struct kernel_lb_addr prealloc_loc = {};
  433. int prealloc_len = 0;
  434. struct udf_inode_info *iinfo;
  435. int err;
  436. /* The previous extent is fake and we should not extend by anything
  437. * - there's nothing to do... */
  438. if (!blocks && fake)
  439. return 0;
  440. iinfo = UDF_I(inode);
  441. /* Round the last extent up to a multiple of block size */
  442. if (last_ext->extLength & (sb->s_blocksize - 1)) {
  443. last_ext->extLength =
  444. (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
  445. (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
  446. sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
  447. iinfo->i_lenExtents =
  448. (iinfo->i_lenExtents + sb->s_blocksize - 1) &
  449. ~(sb->s_blocksize - 1);
  450. }
  451. /* Last extent are just preallocated blocks? */
  452. if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
  453. EXT_NOT_RECORDED_ALLOCATED) {
  454. /* Save the extent so that we can reattach it to the end */
  455. prealloc_loc = last_ext->extLocation;
  456. prealloc_len = last_ext->extLength;
  457. /* Mark the extent as a hole */
  458. last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
  459. (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
  460. last_ext->extLocation.logicalBlockNum = 0;
  461. last_ext->extLocation.partitionReferenceNum = 0;
  462. }
  463. /* Can we merge with the previous extent? */
  464. if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
  465. EXT_NOT_RECORDED_NOT_ALLOCATED) {
  466. add = ((1 << 30) - sb->s_blocksize -
  467. (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >>
  468. sb->s_blocksize_bits;
  469. if (add > blocks)
  470. add = blocks;
  471. blocks -= add;
  472. last_ext->extLength += add << sb->s_blocksize_bits;
  473. }
  474. if (fake) {
  475. udf_add_aext(inode, last_pos, &last_ext->extLocation,
  476. last_ext->extLength, 1);
  477. count++;
  478. } else {
  479. struct kernel_lb_addr tmploc;
  480. uint32_t tmplen;
  481. udf_write_aext(inode, last_pos, &last_ext->extLocation,
  482. last_ext->extLength, 1);
  483. /*
  484. * We've rewritten the last extent but there may be empty
  485. * indirect extent after it - enter it.
  486. */
  487. udf_next_aext(inode, last_pos, &tmploc, &tmplen, 0);
  488. }
  489. /* Managed to do everything necessary? */
  490. if (!blocks)
  491. goto out;
  492. /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
  493. last_ext->extLocation.logicalBlockNum = 0;
  494. last_ext->extLocation.partitionReferenceNum = 0;
  495. add = (1 << (30-sb->s_blocksize_bits)) - 1;
  496. last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
  497. (add << sb->s_blocksize_bits);
  498. /* Create enough extents to cover the whole hole */
  499. while (blocks > add) {
  500. blocks -= add;
  501. err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
  502. last_ext->extLength, 1);
  503. if (err)
  504. return err;
  505. count++;
  506. }
  507. if (blocks) {
  508. last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
  509. (blocks << sb->s_blocksize_bits);
  510. err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
  511. last_ext->extLength, 1);
  512. if (err)
  513. return err;
  514. count++;
  515. }
  516. out:
  517. /* Do we have some preallocated blocks saved? */
  518. if (prealloc_len) {
  519. err = udf_add_aext(inode, last_pos, &prealloc_loc,
  520. prealloc_len, 1);
  521. if (err)
  522. return err;
  523. last_ext->extLocation = prealloc_loc;
  524. last_ext->extLength = prealloc_len;
  525. count++;
  526. }
  527. /* last_pos should point to the last written extent... */
  528. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  529. last_pos->offset -= sizeof(struct short_ad);
  530. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  531. last_pos->offset -= sizeof(struct long_ad);
  532. else
  533. return -EIO;
  534. return count;
  535. }
  536. static int udf_extend_file(struct inode *inode, loff_t newsize)
  537. {
  538. struct extent_position epos;
  539. struct kernel_lb_addr eloc;
  540. uint32_t elen;
  541. int8_t etype;
  542. struct super_block *sb = inode->i_sb;
  543. sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
  544. int adsize;
  545. struct udf_inode_info *iinfo = UDF_I(inode);
  546. struct kernel_long_ad extent;
  547. int err;
  548. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  549. adsize = sizeof(struct short_ad);
  550. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  551. adsize = sizeof(struct long_ad);
  552. else
  553. BUG();
  554. etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
  555. /* File has extent covering the new size (could happen when extending
  556. * inside a block)? */
  557. if (etype != -1)
  558. return 0;
  559. if (newsize & (sb->s_blocksize - 1))
  560. offset++;
  561. /* Extended file just to the boundary of the last file block? */
  562. if (offset == 0)
  563. return 0;
  564. /* Truncate is extending the file by 'offset' blocks */
  565. if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
  566. (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
  567. /* File has no extents at all or has empty last
  568. * indirect extent! Create a fake extent... */
  569. extent.extLocation.logicalBlockNum = 0;
  570. extent.extLocation.partitionReferenceNum = 0;
  571. extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
  572. } else {
  573. epos.offset -= adsize;
  574. etype = udf_next_aext(inode, &epos, &extent.extLocation,
  575. &extent.extLength, 0);
  576. extent.extLength |= etype << 30;
  577. }
  578. err = udf_do_extend_file(inode, &epos, &extent, offset);
  579. if (err < 0)
  580. goto out;
  581. err = 0;
  582. iinfo->i_lenExtents = newsize;
  583. out:
  584. brelse(epos.bh);
  585. return err;
  586. }
  587. static sector_t inode_getblk(struct inode *inode, sector_t block,
  588. int *err, int *new)
  589. {
  590. struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
  591. struct extent_position prev_epos, cur_epos, next_epos;
  592. int count = 0, startnum = 0, endnum = 0;
  593. uint32_t elen = 0, tmpelen;
  594. struct kernel_lb_addr eloc, tmpeloc;
  595. int c = 1;
  596. loff_t lbcount = 0, b_off = 0;
  597. udf_pblk_t newblocknum, newblock;
  598. sector_t offset = 0;
  599. int8_t etype;
  600. struct udf_inode_info *iinfo = UDF_I(inode);
  601. udf_pblk_t goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
  602. int lastblock = 0;
  603. bool isBeyondEOF;
  604. *err = 0;
  605. *new = 0;
  606. prev_epos.offset = udf_file_entry_alloc_offset(inode);
  607. prev_epos.block = iinfo->i_location;
  608. prev_epos.bh = NULL;
  609. cur_epos = next_epos = prev_epos;
  610. b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
  611. /* find the extent which contains the block we are looking for.
  612. alternate between laarr[0] and laarr[1] for locations of the
  613. current extent, and the previous extent */
  614. do {
  615. if (prev_epos.bh != cur_epos.bh) {
  616. brelse(prev_epos.bh);
  617. get_bh(cur_epos.bh);
  618. prev_epos.bh = cur_epos.bh;
  619. }
  620. if (cur_epos.bh != next_epos.bh) {
  621. brelse(cur_epos.bh);
  622. get_bh(next_epos.bh);
  623. cur_epos.bh = next_epos.bh;
  624. }
  625. lbcount += elen;
  626. prev_epos.block = cur_epos.block;
  627. cur_epos.block = next_epos.block;
  628. prev_epos.offset = cur_epos.offset;
  629. cur_epos.offset = next_epos.offset;
  630. etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
  631. if (etype == -1)
  632. break;
  633. c = !c;
  634. laarr[c].extLength = (etype << 30) | elen;
  635. laarr[c].extLocation = eloc;
  636. if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
  637. pgoal = eloc.logicalBlockNum +
  638. ((elen + inode->i_sb->s_blocksize - 1) >>
  639. inode->i_sb->s_blocksize_bits);
  640. count++;
  641. } while (lbcount + elen <= b_off);
  642. b_off -= lbcount;
  643. offset = b_off >> inode->i_sb->s_blocksize_bits;
  644. /*
  645. * Move prev_epos and cur_epos into indirect extent if we are at
  646. * the pointer to it
  647. */
  648. udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
  649. udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
  650. /* if the extent is allocated and recorded, return the block
  651. if the extent is not a multiple of the blocksize, round up */
  652. if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
  653. if (elen & (inode->i_sb->s_blocksize - 1)) {
  654. elen = EXT_RECORDED_ALLOCATED |
  655. ((elen + inode->i_sb->s_blocksize - 1) &
  656. ~(inode->i_sb->s_blocksize - 1));
  657. udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
  658. }
  659. newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
  660. goto out_free;
  661. }
  662. /* Are we beyond EOF? */
  663. if (etype == -1) {
  664. int ret;
  665. isBeyondEOF = true;
  666. if (count) {
  667. if (c)
  668. laarr[0] = laarr[1];
  669. startnum = 1;
  670. } else {
  671. /* Create a fake extent when there's not one */
  672. memset(&laarr[0].extLocation, 0x00,
  673. sizeof(struct kernel_lb_addr));
  674. laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
  675. /* Will udf_do_extend_file() create real extent from
  676. a fake one? */
  677. startnum = (offset > 0);
  678. }
  679. /* Create extents for the hole between EOF and offset */
  680. ret = udf_do_extend_file(inode, &prev_epos, laarr, offset);
  681. if (ret < 0) {
  682. *err = ret;
  683. newblock = 0;
  684. goto out_free;
  685. }
  686. c = 0;
  687. offset = 0;
  688. count += ret;
  689. /* We are not covered by a preallocated extent? */
  690. if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
  691. EXT_NOT_RECORDED_ALLOCATED) {
  692. /* Is there any real extent? - otherwise we overwrite
  693. * the fake one... */
  694. if (count)
  695. c = !c;
  696. laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
  697. inode->i_sb->s_blocksize;
  698. memset(&laarr[c].extLocation, 0x00,
  699. sizeof(struct kernel_lb_addr));
  700. count++;
  701. }
  702. endnum = c + 1;
  703. lastblock = 1;
  704. } else {
  705. isBeyondEOF = false;
  706. endnum = startnum = ((count > 2) ? 2 : count);
  707. /* if the current extent is in position 0,
  708. swap it with the previous */
  709. if (!c && count != 1) {
  710. laarr[2] = laarr[0];
  711. laarr[0] = laarr[1];
  712. laarr[1] = laarr[2];
  713. c = 1;
  714. }
  715. /* if the current block is located in an extent,
  716. read the next extent */
  717. etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
  718. if (etype != -1) {
  719. laarr[c + 1].extLength = (etype << 30) | elen;
  720. laarr[c + 1].extLocation = eloc;
  721. count++;
  722. startnum++;
  723. endnum++;
  724. } else
  725. lastblock = 1;
  726. }
  727. /* if the current extent is not recorded but allocated, get the
  728. * block in the extent corresponding to the requested block */
  729. if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
  730. newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
  731. else { /* otherwise, allocate a new block */
  732. if (iinfo->i_next_alloc_block == block)
  733. goal = iinfo->i_next_alloc_goal;
  734. if (!goal) {
  735. if (!(goal = pgoal)) /* XXX: what was intended here? */
  736. goal = iinfo->i_location.logicalBlockNum + 1;
  737. }
  738. newblocknum = udf_new_block(inode->i_sb, inode,
  739. iinfo->i_location.partitionReferenceNum,
  740. goal, err);
  741. if (!newblocknum) {
  742. *err = -ENOSPC;
  743. newblock = 0;
  744. goto out_free;
  745. }
  746. if (isBeyondEOF)
  747. iinfo->i_lenExtents += inode->i_sb->s_blocksize;
  748. }
  749. /* if the extent the requsted block is located in contains multiple
  750. * blocks, split the extent into at most three extents. blocks prior
  751. * to requested block, requested block, and blocks after requested
  752. * block */
  753. udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
  754. /* We preallocate blocks only for regular files. It also makes sense
  755. * for directories but there's a problem when to drop the
  756. * preallocation. We might use some delayed work for that but I feel
  757. * it's overengineering for a filesystem like UDF. */
  758. if (S_ISREG(inode->i_mode))
  759. udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
  760. /* merge any continuous blocks in laarr */
  761. udf_merge_extents(inode, laarr, &endnum);
  762. /* write back the new extents, inserting new extents if the new number
  763. * of extents is greater than the old number, and deleting extents if
  764. * the new number of extents is less than the old number */
  765. udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
  766. newblock = udf_get_pblock(inode->i_sb, newblocknum,
  767. iinfo->i_location.partitionReferenceNum, 0);
  768. if (!newblock) {
  769. *err = -EIO;
  770. goto out_free;
  771. }
  772. *new = 1;
  773. iinfo->i_next_alloc_block = block;
  774. iinfo->i_next_alloc_goal = newblocknum;
  775. inode->i_ctime = current_time(inode);
  776. if (IS_SYNC(inode))
  777. udf_sync_inode(inode);
  778. else
  779. mark_inode_dirty(inode);
  780. out_free:
  781. brelse(prev_epos.bh);
  782. brelse(cur_epos.bh);
  783. brelse(next_epos.bh);
  784. return newblock;
  785. }
  786. static void udf_split_extents(struct inode *inode, int *c, int offset,
  787. udf_pblk_t newblocknum,
  788. struct kernel_long_ad *laarr, int *endnum)
  789. {
  790. unsigned long blocksize = inode->i_sb->s_blocksize;
  791. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  792. if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
  793. (laarr[*c].extLength >> 30) ==
  794. (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
  795. int curr = *c;
  796. int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
  797. blocksize - 1) >> blocksize_bits;
  798. int8_t etype = (laarr[curr].extLength >> 30);
  799. if (blen == 1)
  800. ;
  801. else if (!offset || blen == offset + 1) {
  802. laarr[curr + 2] = laarr[curr + 1];
  803. laarr[curr + 1] = laarr[curr];
  804. } else {
  805. laarr[curr + 3] = laarr[curr + 1];
  806. laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
  807. }
  808. if (offset) {
  809. if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
  810. udf_free_blocks(inode->i_sb, inode,
  811. &laarr[curr].extLocation,
  812. 0, offset);
  813. laarr[curr].extLength =
  814. EXT_NOT_RECORDED_NOT_ALLOCATED |
  815. (offset << blocksize_bits);
  816. laarr[curr].extLocation.logicalBlockNum = 0;
  817. laarr[curr].extLocation.
  818. partitionReferenceNum = 0;
  819. } else
  820. laarr[curr].extLength = (etype << 30) |
  821. (offset << blocksize_bits);
  822. curr++;
  823. (*c)++;
  824. (*endnum)++;
  825. }
  826. laarr[curr].extLocation.logicalBlockNum = newblocknum;
  827. if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
  828. laarr[curr].extLocation.partitionReferenceNum =
  829. UDF_I(inode)->i_location.partitionReferenceNum;
  830. laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
  831. blocksize;
  832. curr++;
  833. if (blen != offset + 1) {
  834. if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
  835. laarr[curr].extLocation.logicalBlockNum +=
  836. offset + 1;
  837. laarr[curr].extLength = (etype << 30) |
  838. ((blen - (offset + 1)) << blocksize_bits);
  839. curr++;
  840. (*endnum)++;
  841. }
  842. }
  843. }
  844. static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
  845. struct kernel_long_ad *laarr,
  846. int *endnum)
  847. {
  848. int start, length = 0, currlength = 0, i;
  849. if (*endnum >= (c + 1)) {
  850. if (!lastblock)
  851. return;
  852. else
  853. start = c;
  854. } else {
  855. if ((laarr[c + 1].extLength >> 30) ==
  856. (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
  857. start = c + 1;
  858. length = currlength =
  859. (((laarr[c + 1].extLength &
  860. UDF_EXTENT_LENGTH_MASK) +
  861. inode->i_sb->s_blocksize - 1) >>
  862. inode->i_sb->s_blocksize_bits);
  863. } else
  864. start = c;
  865. }
  866. for (i = start + 1; i <= *endnum; i++) {
  867. if (i == *endnum) {
  868. if (lastblock)
  869. length += UDF_DEFAULT_PREALLOC_BLOCKS;
  870. } else if ((laarr[i].extLength >> 30) ==
  871. (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
  872. length += (((laarr[i].extLength &
  873. UDF_EXTENT_LENGTH_MASK) +
  874. inode->i_sb->s_blocksize - 1) >>
  875. inode->i_sb->s_blocksize_bits);
  876. } else
  877. break;
  878. }
  879. if (length) {
  880. int next = laarr[start].extLocation.logicalBlockNum +
  881. (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
  882. inode->i_sb->s_blocksize - 1) >>
  883. inode->i_sb->s_blocksize_bits);
  884. int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
  885. laarr[start].extLocation.partitionReferenceNum,
  886. next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
  887. length : UDF_DEFAULT_PREALLOC_BLOCKS) -
  888. currlength);
  889. if (numalloc) {
  890. if (start == (c + 1))
  891. laarr[start].extLength +=
  892. (numalloc <<
  893. inode->i_sb->s_blocksize_bits);
  894. else {
  895. memmove(&laarr[c + 2], &laarr[c + 1],
  896. sizeof(struct long_ad) * (*endnum - (c + 1)));
  897. (*endnum)++;
  898. laarr[c + 1].extLocation.logicalBlockNum = next;
  899. laarr[c + 1].extLocation.partitionReferenceNum =
  900. laarr[c].extLocation.
  901. partitionReferenceNum;
  902. laarr[c + 1].extLength =
  903. EXT_NOT_RECORDED_ALLOCATED |
  904. (numalloc <<
  905. inode->i_sb->s_blocksize_bits);
  906. start = c + 1;
  907. }
  908. for (i = start + 1; numalloc && i < *endnum; i++) {
  909. int elen = ((laarr[i].extLength &
  910. UDF_EXTENT_LENGTH_MASK) +
  911. inode->i_sb->s_blocksize - 1) >>
  912. inode->i_sb->s_blocksize_bits;
  913. if (elen > numalloc) {
  914. laarr[i].extLength -=
  915. (numalloc <<
  916. inode->i_sb->s_blocksize_bits);
  917. numalloc = 0;
  918. } else {
  919. numalloc -= elen;
  920. if (*endnum > (i + 1))
  921. memmove(&laarr[i],
  922. &laarr[i + 1],
  923. sizeof(struct long_ad) *
  924. (*endnum - (i + 1)));
  925. i--;
  926. (*endnum)--;
  927. }
  928. }
  929. UDF_I(inode)->i_lenExtents +=
  930. numalloc << inode->i_sb->s_blocksize_bits;
  931. }
  932. }
  933. }
  934. static void udf_merge_extents(struct inode *inode, struct kernel_long_ad *laarr,
  935. int *endnum)
  936. {
  937. int i;
  938. unsigned long blocksize = inode->i_sb->s_blocksize;
  939. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  940. for (i = 0; i < (*endnum - 1); i++) {
  941. struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
  942. struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
  943. if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
  944. (((li->extLength >> 30) ==
  945. (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
  946. ((lip1->extLocation.logicalBlockNum -
  947. li->extLocation.logicalBlockNum) ==
  948. (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
  949. blocksize - 1) >> blocksize_bits)))) {
  950. if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
  951. (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
  952. blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
  953. lip1->extLength = (lip1->extLength -
  954. (li->extLength &
  955. UDF_EXTENT_LENGTH_MASK) +
  956. UDF_EXTENT_LENGTH_MASK) &
  957. ~(blocksize - 1);
  958. li->extLength = (li->extLength &
  959. UDF_EXTENT_FLAG_MASK) +
  960. (UDF_EXTENT_LENGTH_MASK + 1) -
  961. blocksize;
  962. lip1->extLocation.logicalBlockNum =
  963. li->extLocation.logicalBlockNum +
  964. ((li->extLength &
  965. UDF_EXTENT_LENGTH_MASK) >>
  966. blocksize_bits);
  967. } else {
  968. li->extLength = lip1->extLength +
  969. (((li->extLength &
  970. UDF_EXTENT_LENGTH_MASK) +
  971. blocksize - 1) & ~(blocksize - 1));
  972. if (*endnum > (i + 2))
  973. memmove(&laarr[i + 1], &laarr[i + 2],
  974. sizeof(struct long_ad) *
  975. (*endnum - (i + 2)));
  976. i--;
  977. (*endnum)--;
  978. }
  979. } else if (((li->extLength >> 30) ==
  980. (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
  981. ((lip1->extLength >> 30) ==
  982. (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
  983. udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
  984. ((li->extLength &
  985. UDF_EXTENT_LENGTH_MASK) +
  986. blocksize - 1) >> blocksize_bits);
  987. li->extLocation.logicalBlockNum = 0;
  988. li->extLocation.partitionReferenceNum = 0;
  989. if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
  990. (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
  991. blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
  992. lip1->extLength = (lip1->extLength -
  993. (li->extLength &
  994. UDF_EXTENT_LENGTH_MASK) +
  995. UDF_EXTENT_LENGTH_MASK) &
  996. ~(blocksize - 1);
  997. li->extLength = (li->extLength &
  998. UDF_EXTENT_FLAG_MASK) +
  999. (UDF_EXTENT_LENGTH_MASK + 1) -
  1000. blocksize;
  1001. } else {
  1002. li->extLength = lip1->extLength +
  1003. (((li->extLength &
  1004. UDF_EXTENT_LENGTH_MASK) +
  1005. blocksize - 1) & ~(blocksize - 1));
  1006. if (*endnum > (i + 2))
  1007. memmove(&laarr[i + 1], &laarr[i + 2],
  1008. sizeof(struct long_ad) *
  1009. (*endnum - (i + 2)));
  1010. i--;
  1011. (*endnum)--;
  1012. }
  1013. } else if ((li->extLength >> 30) ==
  1014. (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
  1015. udf_free_blocks(inode->i_sb, inode,
  1016. &li->extLocation, 0,
  1017. ((li->extLength &
  1018. UDF_EXTENT_LENGTH_MASK) +
  1019. blocksize - 1) >> blocksize_bits);
  1020. li->extLocation.logicalBlockNum = 0;
  1021. li->extLocation.partitionReferenceNum = 0;
  1022. li->extLength = (li->extLength &
  1023. UDF_EXTENT_LENGTH_MASK) |
  1024. EXT_NOT_RECORDED_NOT_ALLOCATED;
  1025. }
  1026. }
  1027. }
  1028. static void udf_update_extents(struct inode *inode, struct kernel_long_ad *laarr,
  1029. int startnum, int endnum,
  1030. struct extent_position *epos)
  1031. {
  1032. int start = 0, i;
  1033. struct kernel_lb_addr tmploc;
  1034. uint32_t tmplen;
  1035. if (startnum > endnum) {
  1036. for (i = 0; i < (startnum - endnum); i++)
  1037. udf_delete_aext(inode, *epos, laarr[i].extLocation,
  1038. laarr[i].extLength);
  1039. } else if (startnum < endnum) {
  1040. for (i = 0; i < (endnum - startnum); i++) {
  1041. udf_insert_aext(inode, *epos, laarr[i].extLocation,
  1042. laarr[i].extLength);
  1043. udf_next_aext(inode, epos, &laarr[i].extLocation,
  1044. &laarr[i].extLength, 1);
  1045. start++;
  1046. }
  1047. }
  1048. for (i = start; i < endnum; i++) {
  1049. udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
  1050. udf_write_aext(inode, epos, &laarr[i].extLocation,
  1051. laarr[i].extLength, 1);
  1052. }
  1053. }
  1054. struct buffer_head *udf_bread(struct inode *inode, udf_pblk_t block,
  1055. int create, int *err)
  1056. {
  1057. struct buffer_head *bh = NULL;
  1058. bh = udf_getblk(inode, block, create, err);
  1059. if (!bh)
  1060. return NULL;
  1061. if (buffer_uptodate(bh))
  1062. return bh;
  1063. ll_rw_block(REQ_OP_READ, 0, 1, &bh);
  1064. wait_on_buffer(bh);
  1065. if (buffer_uptodate(bh))
  1066. return bh;
  1067. brelse(bh);
  1068. *err = -EIO;
  1069. return NULL;
  1070. }
  1071. int udf_setsize(struct inode *inode, loff_t newsize)
  1072. {
  1073. int err;
  1074. struct udf_inode_info *iinfo;
  1075. int bsize = i_blocksize(inode);
  1076. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1077. S_ISLNK(inode->i_mode)))
  1078. return -EINVAL;
  1079. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1080. return -EPERM;
  1081. iinfo = UDF_I(inode);
  1082. if (newsize > inode->i_size) {
  1083. down_write(&iinfo->i_data_sem);
  1084. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
  1085. if (bsize <
  1086. (udf_file_entry_alloc_offset(inode) + newsize)) {
  1087. err = udf_expand_file_adinicb(inode);
  1088. if (err)
  1089. return err;
  1090. down_write(&iinfo->i_data_sem);
  1091. } else {
  1092. iinfo->i_lenAlloc = newsize;
  1093. goto set_size;
  1094. }
  1095. }
  1096. err = udf_extend_file(inode, newsize);
  1097. if (err) {
  1098. up_write(&iinfo->i_data_sem);
  1099. return err;
  1100. }
  1101. set_size:
  1102. up_write(&iinfo->i_data_sem);
  1103. truncate_setsize(inode, newsize);
  1104. } else {
  1105. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
  1106. down_write(&iinfo->i_data_sem);
  1107. udf_clear_extent_cache(inode);
  1108. memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + newsize,
  1109. 0x00, bsize - newsize -
  1110. udf_file_entry_alloc_offset(inode));
  1111. iinfo->i_lenAlloc = newsize;
  1112. truncate_setsize(inode, newsize);
  1113. up_write(&iinfo->i_data_sem);
  1114. goto update_time;
  1115. }
  1116. err = block_truncate_page(inode->i_mapping, newsize,
  1117. udf_get_block);
  1118. if (err)
  1119. return err;
  1120. truncate_setsize(inode, newsize);
  1121. down_write(&iinfo->i_data_sem);
  1122. udf_clear_extent_cache(inode);
  1123. udf_truncate_extents(inode);
  1124. up_write(&iinfo->i_data_sem);
  1125. }
  1126. update_time:
  1127. inode->i_mtime = inode->i_ctime = current_time(inode);
  1128. if (IS_SYNC(inode))
  1129. udf_sync_inode(inode);
  1130. else
  1131. mark_inode_dirty(inode);
  1132. return 0;
  1133. }
  1134. /*
  1135. * Maximum length of linked list formed by ICB hierarchy. The chosen number is
  1136. * arbitrary - just that we hopefully don't limit any real use of rewritten
  1137. * inode on write-once media but avoid looping for too long on corrupted media.
  1138. */
  1139. #define UDF_MAX_ICB_NESTING 1024
  1140. static int udf_read_inode(struct inode *inode, bool hidden_inode)
  1141. {
  1142. struct buffer_head *bh = NULL;
  1143. struct fileEntry *fe;
  1144. struct extendedFileEntry *efe;
  1145. uint16_t ident;
  1146. struct udf_inode_info *iinfo = UDF_I(inode);
  1147. struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
  1148. struct kernel_lb_addr *iloc = &iinfo->i_location;
  1149. unsigned int link_count;
  1150. unsigned int indirections = 0;
  1151. int bs = inode->i_sb->s_blocksize;
  1152. int ret = -EIO;
  1153. reread:
  1154. if (iloc->partitionReferenceNum >= sbi->s_partitions) {
  1155. udf_debug("partition reference: %d > logical volume partitions: %d\n",
  1156. iloc->partitionReferenceNum, sbi->s_partitions);
  1157. return -EIO;
  1158. }
  1159. if (iloc->logicalBlockNum >=
  1160. sbi->s_partmaps[iloc->partitionReferenceNum].s_partition_len) {
  1161. udf_debug("block=%d, partition=%d out of range\n",
  1162. iloc->logicalBlockNum, iloc->partitionReferenceNum);
  1163. return -EIO;
  1164. }
  1165. /*
  1166. * Set defaults, but the inode is still incomplete!
  1167. * Note: get_new_inode() sets the following on a new inode:
  1168. * i_sb = sb
  1169. * i_no = ino
  1170. * i_flags = sb->s_flags
  1171. * i_state = 0
  1172. * clean_inode(): zero fills and sets
  1173. * i_count = 1
  1174. * i_nlink = 1
  1175. * i_op = NULL;
  1176. */
  1177. bh = udf_read_ptagged(inode->i_sb, iloc, 0, &ident);
  1178. if (!bh) {
  1179. udf_err(inode->i_sb, "(ino %ld) failed !bh\n", inode->i_ino);
  1180. return -EIO;
  1181. }
  1182. if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
  1183. ident != TAG_IDENT_USE) {
  1184. udf_err(inode->i_sb, "(ino %ld) failed ident=%d\n",
  1185. inode->i_ino, ident);
  1186. goto out;
  1187. }
  1188. fe = (struct fileEntry *)bh->b_data;
  1189. efe = (struct extendedFileEntry *)bh->b_data;
  1190. if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
  1191. struct buffer_head *ibh;
  1192. ibh = udf_read_ptagged(inode->i_sb, iloc, 1, &ident);
  1193. if (ident == TAG_IDENT_IE && ibh) {
  1194. struct kernel_lb_addr loc;
  1195. struct indirectEntry *ie;
  1196. ie = (struct indirectEntry *)ibh->b_data;
  1197. loc = lelb_to_cpu(ie->indirectICB.extLocation);
  1198. if (ie->indirectICB.extLength) {
  1199. brelse(ibh);
  1200. memcpy(&iinfo->i_location, &loc,
  1201. sizeof(struct kernel_lb_addr));
  1202. if (++indirections > UDF_MAX_ICB_NESTING) {
  1203. udf_err(inode->i_sb,
  1204. "too many ICBs in ICB hierarchy"
  1205. " (max %d supported)\n",
  1206. UDF_MAX_ICB_NESTING);
  1207. goto out;
  1208. }
  1209. brelse(bh);
  1210. goto reread;
  1211. }
  1212. }
  1213. brelse(ibh);
  1214. } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
  1215. udf_err(inode->i_sb, "unsupported strategy type: %d\n",
  1216. le16_to_cpu(fe->icbTag.strategyType));
  1217. goto out;
  1218. }
  1219. if (fe->icbTag.strategyType == cpu_to_le16(4))
  1220. iinfo->i_strat4096 = 0;
  1221. else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
  1222. iinfo->i_strat4096 = 1;
  1223. iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
  1224. ICBTAG_FLAG_AD_MASK;
  1225. iinfo->i_unique = 0;
  1226. iinfo->i_lenEAttr = 0;
  1227. iinfo->i_lenExtents = 0;
  1228. iinfo->i_lenAlloc = 0;
  1229. iinfo->i_next_alloc_block = 0;
  1230. iinfo->i_next_alloc_goal = 0;
  1231. if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
  1232. iinfo->i_efe = 1;
  1233. iinfo->i_use = 0;
  1234. ret = udf_alloc_i_data(inode, bs -
  1235. sizeof(struct extendedFileEntry));
  1236. if (ret)
  1237. goto out;
  1238. memcpy(iinfo->i_ext.i_data,
  1239. bh->b_data + sizeof(struct extendedFileEntry),
  1240. bs - sizeof(struct extendedFileEntry));
  1241. } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
  1242. iinfo->i_efe = 0;
  1243. iinfo->i_use = 0;
  1244. ret = udf_alloc_i_data(inode, bs - sizeof(struct fileEntry));
  1245. if (ret)
  1246. goto out;
  1247. memcpy(iinfo->i_ext.i_data,
  1248. bh->b_data + sizeof(struct fileEntry),
  1249. bs - sizeof(struct fileEntry));
  1250. } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
  1251. iinfo->i_efe = 0;
  1252. iinfo->i_use = 1;
  1253. iinfo->i_lenAlloc = le32_to_cpu(
  1254. ((struct unallocSpaceEntry *)bh->b_data)->
  1255. lengthAllocDescs);
  1256. ret = udf_alloc_i_data(inode, bs -
  1257. sizeof(struct unallocSpaceEntry));
  1258. if (ret)
  1259. goto out;
  1260. memcpy(iinfo->i_ext.i_data,
  1261. bh->b_data + sizeof(struct unallocSpaceEntry),
  1262. bs - sizeof(struct unallocSpaceEntry));
  1263. return 0;
  1264. }
  1265. ret = -EIO;
  1266. read_lock(&sbi->s_cred_lock);
  1267. i_uid_write(inode, le32_to_cpu(fe->uid));
  1268. if (!uid_valid(inode->i_uid) ||
  1269. UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
  1270. UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
  1271. inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
  1272. i_gid_write(inode, le32_to_cpu(fe->gid));
  1273. if (!gid_valid(inode->i_gid) ||
  1274. UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
  1275. UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
  1276. inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
  1277. if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
  1278. sbi->s_fmode != UDF_INVALID_MODE)
  1279. inode->i_mode = sbi->s_fmode;
  1280. else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
  1281. sbi->s_dmode != UDF_INVALID_MODE)
  1282. inode->i_mode = sbi->s_dmode;
  1283. else
  1284. inode->i_mode = udf_convert_permissions(fe);
  1285. inode->i_mode &= ~sbi->s_umask;
  1286. read_unlock(&sbi->s_cred_lock);
  1287. link_count = le16_to_cpu(fe->fileLinkCount);
  1288. if (!link_count) {
  1289. if (!hidden_inode) {
  1290. ret = -ESTALE;
  1291. goto out;
  1292. }
  1293. link_count = 1;
  1294. }
  1295. set_nlink(inode, link_count);
  1296. inode->i_size = le64_to_cpu(fe->informationLength);
  1297. iinfo->i_lenExtents = inode->i_size;
  1298. if (iinfo->i_efe == 0) {
  1299. inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
  1300. (inode->i_sb->s_blocksize_bits - 9);
  1301. if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
  1302. inode->i_atime = sbi->s_record_time;
  1303. if (!udf_disk_stamp_to_time(&inode->i_mtime,
  1304. fe->modificationTime))
  1305. inode->i_mtime = sbi->s_record_time;
  1306. if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
  1307. inode->i_ctime = sbi->s_record_time;
  1308. iinfo->i_unique = le64_to_cpu(fe->uniqueID);
  1309. iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
  1310. iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
  1311. iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
  1312. } else {
  1313. inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
  1314. (inode->i_sb->s_blocksize_bits - 9);
  1315. if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
  1316. inode->i_atime = sbi->s_record_time;
  1317. if (!udf_disk_stamp_to_time(&inode->i_mtime,
  1318. efe->modificationTime))
  1319. inode->i_mtime = sbi->s_record_time;
  1320. if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
  1321. iinfo->i_crtime = sbi->s_record_time;
  1322. if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
  1323. inode->i_ctime = sbi->s_record_time;
  1324. iinfo->i_unique = le64_to_cpu(efe->uniqueID);
  1325. iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
  1326. iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
  1327. iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
  1328. }
  1329. inode->i_generation = iinfo->i_unique;
  1330. /*
  1331. * Sanity check length of allocation descriptors and extended attrs to
  1332. * avoid integer overflows
  1333. */
  1334. if (iinfo->i_lenEAttr > bs || iinfo->i_lenAlloc > bs)
  1335. goto out;
  1336. /* Now do exact checks */
  1337. if (udf_file_entry_alloc_offset(inode) + iinfo->i_lenAlloc > bs)
  1338. goto out;
  1339. /* Sanity checks for files in ICB so that we don't get confused later */
  1340. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
  1341. /*
  1342. * For file in ICB data is stored in allocation descriptor
  1343. * so sizes should match
  1344. */
  1345. if (iinfo->i_lenAlloc != inode->i_size)
  1346. goto out;
  1347. /* File in ICB has to fit in there... */
  1348. if (inode->i_size > bs - udf_file_entry_alloc_offset(inode))
  1349. goto out;
  1350. }
  1351. switch (fe->icbTag.fileType) {
  1352. case ICBTAG_FILE_TYPE_DIRECTORY:
  1353. inode->i_op = &udf_dir_inode_operations;
  1354. inode->i_fop = &udf_dir_operations;
  1355. inode->i_mode |= S_IFDIR;
  1356. inc_nlink(inode);
  1357. break;
  1358. case ICBTAG_FILE_TYPE_REALTIME:
  1359. case ICBTAG_FILE_TYPE_REGULAR:
  1360. case ICBTAG_FILE_TYPE_UNDEF:
  1361. case ICBTAG_FILE_TYPE_VAT20:
  1362. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
  1363. inode->i_data.a_ops = &udf_adinicb_aops;
  1364. else
  1365. inode->i_data.a_ops = &udf_aops;
  1366. inode->i_op = &udf_file_inode_operations;
  1367. inode->i_fop = &udf_file_operations;
  1368. inode->i_mode |= S_IFREG;
  1369. break;
  1370. case ICBTAG_FILE_TYPE_BLOCK:
  1371. inode->i_mode |= S_IFBLK;
  1372. break;
  1373. case ICBTAG_FILE_TYPE_CHAR:
  1374. inode->i_mode |= S_IFCHR;
  1375. break;
  1376. case ICBTAG_FILE_TYPE_FIFO:
  1377. init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
  1378. break;
  1379. case ICBTAG_FILE_TYPE_SOCKET:
  1380. init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
  1381. break;
  1382. case ICBTAG_FILE_TYPE_SYMLINK:
  1383. inode->i_data.a_ops = &udf_symlink_aops;
  1384. inode->i_op = &udf_symlink_inode_operations;
  1385. inode_nohighmem(inode);
  1386. inode->i_mode = S_IFLNK | 0777;
  1387. break;
  1388. case ICBTAG_FILE_TYPE_MAIN:
  1389. udf_debug("METADATA FILE-----\n");
  1390. break;
  1391. case ICBTAG_FILE_TYPE_MIRROR:
  1392. udf_debug("METADATA MIRROR FILE-----\n");
  1393. break;
  1394. case ICBTAG_FILE_TYPE_BITMAP:
  1395. udf_debug("METADATA BITMAP FILE-----\n");
  1396. break;
  1397. default:
  1398. udf_err(inode->i_sb, "(ino %ld) failed unknown file type=%d\n",
  1399. inode->i_ino, fe->icbTag.fileType);
  1400. goto out;
  1401. }
  1402. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1403. struct deviceSpec *dsea =
  1404. (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
  1405. if (dsea) {
  1406. init_special_inode(inode, inode->i_mode,
  1407. MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
  1408. le32_to_cpu(dsea->minorDeviceIdent)));
  1409. /* Developer ID ??? */
  1410. } else
  1411. goto out;
  1412. }
  1413. ret = 0;
  1414. out:
  1415. brelse(bh);
  1416. return ret;
  1417. }
  1418. static int udf_alloc_i_data(struct inode *inode, size_t size)
  1419. {
  1420. struct udf_inode_info *iinfo = UDF_I(inode);
  1421. iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
  1422. if (!iinfo->i_ext.i_data)
  1423. return -ENOMEM;
  1424. return 0;
  1425. }
  1426. static umode_t udf_convert_permissions(struct fileEntry *fe)
  1427. {
  1428. umode_t mode;
  1429. uint32_t permissions;
  1430. uint32_t flags;
  1431. permissions = le32_to_cpu(fe->permissions);
  1432. flags = le16_to_cpu(fe->icbTag.flags);
  1433. mode = ((permissions) & 0007) |
  1434. ((permissions >> 2) & 0070) |
  1435. ((permissions >> 4) & 0700) |
  1436. ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
  1437. ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
  1438. ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
  1439. return mode;
  1440. }
  1441. int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
  1442. {
  1443. return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
  1444. }
  1445. static int udf_sync_inode(struct inode *inode)
  1446. {
  1447. return udf_update_inode(inode, 1);
  1448. }
  1449. static void udf_adjust_time(struct udf_inode_info *iinfo, struct timespec time)
  1450. {
  1451. if (iinfo->i_crtime.tv_sec > time.tv_sec ||
  1452. (iinfo->i_crtime.tv_sec == time.tv_sec &&
  1453. iinfo->i_crtime.tv_nsec > time.tv_nsec))
  1454. iinfo->i_crtime = time;
  1455. }
  1456. static int udf_update_inode(struct inode *inode, int do_sync)
  1457. {
  1458. struct buffer_head *bh = NULL;
  1459. struct fileEntry *fe;
  1460. struct extendedFileEntry *efe;
  1461. uint64_t lb_recorded;
  1462. uint32_t udfperms;
  1463. uint16_t icbflags;
  1464. uint16_t crclen;
  1465. int err = 0;
  1466. struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
  1467. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  1468. struct udf_inode_info *iinfo = UDF_I(inode);
  1469. bh = udf_tgetblk(inode->i_sb,
  1470. udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
  1471. if (!bh) {
  1472. udf_debug("getblk failure\n");
  1473. return -EIO;
  1474. }
  1475. lock_buffer(bh);
  1476. memset(bh->b_data, 0, inode->i_sb->s_blocksize);
  1477. fe = (struct fileEntry *)bh->b_data;
  1478. efe = (struct extendedFileEntry *)bh->b_data;
  1479. if (iinfo->i_use) {
  1480. struct unallocSpaceEntry *use =
  1481. (struct unallocSpaceEntry *)bh->b_data;
  1482. use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
  1483. memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
  1484. iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
  1485. sizeof(struct unallocSpaceEntry));
  1486. use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
  1487. crclen = sizeof(struct unallocSpaceEntry);
  1488. goto finish;
  1489. }
  1490. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
  1491. fe->uid = cpu_to_le32(-1);
  1492. else
  1493. fe->uid = cpu_to_le32(i_uid_read(inode));
  1494. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
  1495. fe->gid = cpu_to_le32(-1);
  1496. else
  1497. fe->gid = cpu_to_le32(i_gid_read(inode));
  1498. udfperms = ((inode->i_mode & 0007)) |
  1499. ((inode->i_mode & 0070) << 2) |
  1500. ((inode->i_mode & 0700) << 4);
  1501. udfperms |= (le32_to_cpu(fe->permissions) &
  1502. (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
  1503. FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
  1504. FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
  1505. fe->permissions = cpu_to_le32(udfperms);
  1506. if (S_ISDIR(inode->i_mode) && inode->i_nlink > 0)
  1507. fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
  1508. else
  1509. fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
  1510. fe->informationLength = cpu_to_le64(inode->i_size);
  1511. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1512. struct regid *eid;
  1513. struct deviceSpec *dsea =
  1514. (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
  1515. if (!dsea) {
  1516. dsea = (struct deviceSpec *)
  1517. udf_add_extendedattr(inode,
  1518. sizeof(struct deviceSpec) +
  1519. sizeof(struct regid), 12, 0x3);
  1520. dsea->attrType = cpu_to_le32(12);
  1521. dsea->attrSubtype = 1;
  1522. dsea->attrLength = cpu_to_le32(
  1523. sizeof(struct deviceSpec) +
  1524. sizeof(struct regid));
  1525. dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
  1526. }
  1527. eid = (struct regid *)dsea->impUse;
  1528. memset(eid, 0, sizeof(*eid));
  1529. strcpy(eid->ident, UDF_ID_DEVELOPER);
  1530. eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
  1531. eid->identSuffix[1] = UDF_OS_ID_LINUX;
  1532. dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
  1533. dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
  1534. }
  1535. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
  1536. lb_recorded = 0; /* No extents => no blocks! */
  1537. else
  1538. lb_recorded =
  1539. (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
  1540. (blocksize_bits - 9);
  1541. if (iinfo->i_efe == 0) {
  1542. memcpy(bh->b_data + sizeof(struct fileEntry),
  1543. iinfo->i_ext.i_data,
  1544. inode->i_sb->s_blocksize - sizeof(struct fileEntry));
  1545. fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
  1546. udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
  1547. udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
  1548. udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
  1549. memset(&(fe->impIdent), 0, sizeof(struct regid));
  1550. strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
  1551. fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1552. fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1553. fe->uniqueID = cpu_to_le64(iinfo->i_unique);
  1554. fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
  1555. fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
  1556. fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
  1557. fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
  1558. crclen = sizeof(struct fileEntry);
  1559. } else {
  1560. memcpy(bh->b_data + sizeof(struct extendedFileEntry),
  1561. iinfo->i_ext.i_data,
  1562. inode->i_sb->s_blocksize -
  1563. sizeof(struct extendedFileEntry));
  1564. efe->objectSize = cpu_to_le64(inode->i_size);
  1565. efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
  1566. udf_adjust_time(iinfo, inode->i_atime);
  1567. udf_adjust_time(iinfo, inode->i_mtime);
  1568. udf_adjust_time(iinfo, inode->i_ctime);
  1569. udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
  1570. udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
  1571. udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
  1572. udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
  1573. memset(&(efe->impIdent), 0, sizeof(efe->impIdent));
  1574. strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
  1575. efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1576. efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1577. efe->uniqueID = cpu_to_le64(iinfo->i_unique);
  1578. efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
  1579. efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
  1580. efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
  1581. efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
  1582. crclen = sizeof(struct extendedFileEntry);
  1583. }
  1584. finish:
  1585. if (iinfo->i_strat4096) {
  1586. fe->icbTag.strategyType = cpu_to_le16(4096);
  1587. fe->icbTag.strategyParameter = cpu_to_le16(1);
  1588. fe->icbTag.numEntries = cpu_to_le16(2);
  1589. } else {
  1590. fe->icbTag.strategyType = cpu_to_le16(4);
  1591. fe->icbTag.numEntries = cpu_to_le16(1);
  1592. }
  1593. if (iinfo->i_use)
  1594. fe->icbTag.fileType = ICBTAG_FILE_TYPE_USE;
  1595. else if (S_ISDIR(inode->i_mode))
  1596. fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
  1597. else if (S_ISREG(inode->i_mode))
  1598. fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
  1599. else if (S_ISLNK(inode->i_mode))
  1600. fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
  1601. else if (S_ISBLK(inode->i_mode))
  1602. fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
  1603. else if (S_ISCHR(inode->i_mode))
  1604. fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
  1605. else if (S_ISFIFO(inode->i_mode))
  1606. fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
  1607. else if (S_ISSOCK(inode->i_mode))
  1608. fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
  1609. icbflags = iinfo->i_alloc_type |
  1610. ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
  1611. ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
  1612. ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
  1613. (le16_to_cpu(fe->icbTag.flags) &
  1614. ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
  1615. ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
  1616. fe->icbTag.flags = cpu_to_le16(icbflags);
  1617. if (sbi->s_udfrev >= 0x0200)
  1618. fe->descTag.descVersion = cpu_to_le16(3);
  1619. else
  1620. fe->descTag.descVersion = cpu_to_le16(2);
  1621. fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
  1622. fe->descTag.tagLocation = cpu_to_le32(
  1623. iinfo->i_location.logicalBlockNum);
  1624. crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
  1625. fe->descTag.descCRCLength = cpu_to_le16(crclen);
  1626. fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
  1627. crclen));
  1628. fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
  1629. set_buffer_uptodate(bh);
  1630. unlock_buffer(bh);
  1631. /* write the data blocks */
  1632. mark_buffer_dirty(bh);
  1633. if (do_sync) {
  1634. sync_dirty_buffer(bh);
  1635. if (buffer_write_io_error(bh)) {
  1636. udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
  1637. inode->i_ino);
  1638. err = -EIO;
  1639. }
  1640. }
  1641. brelse(bh);
  1642. return err;
  1643. }
  1644. struct inode *__udf_iget(struct super_block *sb, struct kernel_lb_addr *ino,
  1645. bool hidden_inode)
  1646. {
  1647. unsigned long block = udf_get_lb_pblock(sb, ino, 0);
  1648. struct inode *inode = iget_locked(sb, block);
  1649. int err;
  1650. if (!inode)
  1651. return ERR_PTR(-ENOMEM);
  1652. if (!(inode->i_state & I_NEW))
  1653. return inode;
  1654. memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
  1655. err = udf_read_inode(inode, hidden_inode);
  1656. if (err < 0) {
  1657. iget_failed(inode);
  1658. return ERR_PTR(err);
  1659. }
  1660. unlock_new_inode(inode);
  1661. return inode;
  1662. }
  1663. int udf_setup_indirect_aext(struct inode *inode, udf_pblk_t block,
  1664. struct extent_position *epos)
  1665. {
  1666. struct super_block *sb = inode->i_sb;
  1667. struct buffer_head *bh;
  1668. struct allocExtDesc *aed;
  1669. struct extent_position nepos;
  1670. struct kernel_lb_addr neloc;
  1671. int ver, adsize;
  1672. if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  1673. adsize = sizeof(struct short_ad);
  1674. else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  1675. adsize = sizeof(struct long_ad);
  1676. else
  1677. return -EIO;
  1678. neloc.logicalBlockNum = block;
  1679. neloc.partitionReferenceNum = epos->block.partitionReferenceNum;
  1680. bh = udf_tgetblk(sb, udf_get_lb_pblock(sb, &neloc, 0));
  1681. if (!bh)
  1682. return -EIO;
  1683. lock_buffer(bh);
  1684. memset(bh->b_data, 0x00, sb->s_blocksize);
  1685. set_buffer_uptodate(bh);
  1686. unlock_buffer(bh);
  1687. mark_buffer_dirty_inode(bh, inode);
  1688. aed = (struct allocExtDesc *)(bh->b_data);
  1689. if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT)) {
  1690. aed->previousAllocExtLocation =
  1691. cpu_to_le32(epos->block.logicalBlockNum);
  1692. }
  1693. aed->lengthAllocDescs = cpu_to_le32(0);
  1694. if (UDF_SB(sb)->s_udfrev >= 0x0200)
  1695. ver = 3;
  1696. else
  1697. ver = 2;
  1698. udf_new_tag(bh->b_data, TAG_IDENT_AED, ver, 1, block,
  1699. sizeof(struct tag));
  1700. nepos.block = neloc;
  1701. nepos.offset = sizeof(struct allocExtDesc);
  1702. nepos.bh = bh;
  1703. /*
  1704. * Do we have to copy current last extent to make space for indirect
  1705. * one?
  1706. */
  1707. if (epos->offset + adsize > sb->s_blocksize) {
  1708. struct kernel_lb_addr cp_loc;
  1709. uint32_t cp_len;
  1710. int cp_type;
  1711. epos->offset -= adsize;
  1712. cp_type = udf_current_aext(inode, epos, &cp_loc, &cp_len, 0);
  1713. cp_len |= ((uint32_t)cp_type) << 30;
  1714. __udf_add_aext(inode, &nepos, &cp_loc, cp_len, 1);
  1715. udf_write_aext(inode, epos, &nepos.block,
  1716. sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
  1717. } else {
  1718. __udf_add_aext(inode, epos, &nepos.block,
  1719. sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
  1720. }
  1721. brelse(epos->bh);
  1722. *epos = nepos;
  1723. return 0;
  1724. }
  1725. /*
  1726. * Append extent at the given position - should be the first free one in inode
  1727. * / indirect extent. This function assumes there is enough space in the inode
  1728. * or indirect extent. Use udf_add_aext() if you didn't check for this before.
  1729. */
  1730. int __udf_add_aext(struct inode *inode, struct extent_position *epos,
  1731. struct kernel_lb_addr *eloc, uint32_t elen, int inc)
  1732. {
  1733. struct udf_inode_info *iinfo = UDF_I(inode);
  1734. struct allocExtDesc *aed;
  1735. int adsize;
  1736. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  1737. adsize = sizeof(struct short_ad);
  1738. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  1739. adsize = sizeof(struct long_ad);
  1740. else
  1741. return -EIO;
  1742. if (!epos->bh) {
  1743. WARN_ON(iinfo->i_lenAlloc !=
  1744. epos->offset - udf_file_entry_alloc_offset(inode));
  1745. } else {
  1746. aed = (struct allocExtDesc *)epos->bh->b_data;
  1747. WARN_ON(le32_to_cpu(aed->lengthAllocDescs) !=
  1748. epos->offset - sizeof(struct allocExtDesc));
  1749. WARN_ON(epos->offset + adsize > inode->i_sb->s_blocksize);
  1750. }
  1751. udf_write_aext(inode, epos, eloc, elen, inc);
  1752. if (!epos->bh) {
  1753. iinfo->i_lenAlloc += adsize;
  1754. mark_inode_dirty(inode);
  1755. } else {
  1756. aed = (struct allocExtDesc *)epos->bh->b_data;
  1757. le32_add_cpu(&aed->lengthAllocDescs, adsize);
  1758. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1759. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
  1760. udf_update_tag(epos->bh->b_data,
  1761. epos->offset + (inc ? 0 : adsize));
  1762. else
  1763. udf_update_tag(epos->bh->b_data,
  1764. sizeof(struct allocExtDesc));
  1765. mark_buffer_dirty_inode(epos->bh, inode);
  1766. }
  1767. return 0;
  1768. }
  1769. /*
  1770. * Append extent at given position - should be the first free one in inode
  1771. * / indirect extent. Takes care of allocating and linking indirect blocks.
  1772. */
  1773. int udf_add_aext(struct inode *inode, struct extent_position *epos,
  1774. struct kernel_lb_addr *eloc, uint32_t elen, int inc)
  1775. {
  1776. int adsize;
  1777. struct super_block *sb = inode->i_sb;
  1778. if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  1779. adsize = sizeof(struct short_ad);
  1780. else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  1781. adsize = sizeof(struct long_ad);
  1782. else
  1783. return -EIO;
  1784. if (epos->offset + (2 * adsize) > sb->s_blocksize) {
  1785. int err;
  1786. udf_pblk_t new_block;
  1787. new_block = udf_new_block(sb, NULL,
  1788. epos->block.partitionReferenceNum,
  1789. epos->block.logicalBlockNum, &err);
  1790. if (!new_block)
  1791. return -ENOSPC;
  1792. err = udf_setup_indirect_aext(inode, new_block, epos);
  1793. if (err)
  1794. return err;
  1795. }
  1796. return __udf_add_aext(inode, epos, eloc, elen, inc);
  1797. }
  1798. void udf_write_aext(struct inode *inode, struct extent_position *epos,
  1799. struct kernel_lb_addr *eloc, uint32_t elen, int inc)
  1800. {
  1801. int adsize;
  1802. uint8_t *ptr;
  1803. struct short_ad *sad;
  1804. struct long_ad *lad;
  1805. struct udf_inode_info *iinfo = UDF_I(inode);
  1806. if (!epos->bh)
  1807. ptr = iinfo->i_ext.i_data + epos->offset -
  1808. udf_file_entry_alloc_offset(inode) +
  1809. iinfo->i_lenEAttr;
  1810. else
  1811. ptr = epos->bh->b_data + epos->offset;
  1812. switch (iinfo->i_alloc_type) {
  1813. case ICBTAG_FLAG_AD_SHORT:
  1814. sad = (struct short_ad *)ptr;
  1815. sad->extLength = cpu_to_le32(elen);
  1816. sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
  1817. adsize = sizeof(struct short_ad);
  1818. break;
  1819. case ICBTAG_FLAG_AD_LONG:
  1820. lad = (struct long_ad *)ptr;
  1821. lad->extLength = cpu_to_le32(elen);
  1822. lad->extLocation = cpu_to_lelb(*eloc);
  1823. memset(lad->impUse, 0x00, sizeof(lad->impUse));
  1824. adsize = sizeof(struct long_ad);
  1825. break;
  1826. default:
  1827. return;
  1828. }
  1829. if (epos->bh) {
  1830. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1831. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
  1832. struct allocExtDesc *aed =
  1833. (struct allocExtDesc *)epos->bh->b_data;
  1834. udf_update_tag(epos->bh->b_data,
  1835. le32_to_cpu(aed->lengthAllocDescs) +
  1836. sizeof(struct allocExtDesc));
  1837. }
  1838. mark_buffer_dirty_inode(epos->bh, inode);
  1839. } else {
  1840. mark_inode_dirty(inode);
  1841. }
  1842. if (inc)
  1843. epos->offset += adsize;
  1844. }
  1845. /*
  1846. * Only 1 indirect extent in a row really makes sense but allow upto 16 in case
  1847. * someone does some weird stuff.
  1848. */
  1849. #define UDF_MAX_INDIR_EXTS 16
  1850. int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
  1851. struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
  1852. {
  1853. int8_t etype;
  1854. unsigned int indirections = 0;
  1855. while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
  1856. (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
  1857. udf_pblk_t block;
  1858. if (++indirections > UDF_MAX_INDIR_EXTS) {
  1859. udf_err(inode->i_sb,
  1860. "too many indirect extents in inode %lu\n",
  1861. inode->i_ino);
  1862. return -1;
  1863. }
  1864. epos->block = *eloc;
  1865. epos->offset = sizeof(struct allocExtDesc);
  1866. brelse(epos->bh);
  1867. block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
  1868. epos->bh = udf_tread(inode->i_sb, block);
  1869. if (!epos->bh) {
  1870. udf_debug("reading block %d failed!\n", block);
  1871. return -1;
  1872. }
  1873. }
  1874. return etype;
  1875. }
  1876. int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
  1877. struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
  1878. {
  1879. int alen;
  1880. int8_t etype;
  1881. uint8_t *ptr;
  1882. struct short_ad *sad;
  1883. struct long_ad *lad;
  1884. struct udf_inode_info *iinfo = UDF_I(inode);
  1885. if (!epos->bh) {
  1886. if (!epos->offset)
  1887. epos->offset = udf_file_entry_alloc_offset(inode);
  1888. ptr = iinfo->i_ext.i_data + epos->offset -
  1889. udf_file_entry_alloc_offset(inode) +
  1890. iinfo->i_lenEAttr;
  1891. alen = udf_file_entry_alloc_offset(inode) +
  1892. iinfo->i_lenAlloc;
  1893. } else {
  1894. if (!epos->offset)
  1895. epos->offset = sizeof(struct allocExtDesc);
  1896. ptr = epos->bh->b_data + epos->offset;
  1897. alen = sizeof(struct allocExtDesc) +
  1898. le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
  1899. lengthAllocDescs);
  1900. }
  1901. switch (iinfo->i_alloc_type) {
  1902. case ICBTAG_FLAG_AD_SHORT:
  1903. sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
  1904. if (!sad)
  1905. return -1;
  1906. etype = le32_to_cpu(sad->extLength) >> 30;
  1907. eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
  1908. eloc->partitionReferenceNum =
  1909. iinfo->i_location.partitionReferenceNum;
  1910. *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
  1911. break;
  1912. case ICBTAG_FLAG_AD_LONG:
  1913. lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
  1914. if (!lad)
  1915. return -1;
  1916. etype = le32_to_cpu(lad->extLength) >> 30;
  1917. *eloc = lelb_to_cpu(lad->extLocation);
  1918. *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
  1919. break;
  1920. default:
  1921. udf_debug("alloc_type = %d unsupported\n", iinfo->i_alloc_type);
  1922. return -1;
  1923. }
  1924. return etype;
  1925. }
  1926. static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
  1927. struct kernel_lb_addr neloc, uint32_t nelen)
  1928. {
  1929. struct kernel_lb_addr oeloc;
  1930. uint32_t oelen;
  1931. int8_t etype;
  1932. if (epos.bh)
  1933. get_bh(epos.bh);
  1934. while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
  1935. udf_write_aext(inode, &epos, &neloc, nelen, 1);
  1936. neloc = oeloc;
  1937. nelen = (etype << 30) | oelen;
  1938. }
  1939. udf_add_aext(inode, &epos, &neloc, nelen, 1);
  1940. brelse(epos.bh);
  1941. return (nelen >> 30);
  1942. }
  1943. int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
  1944. struct kernel_lb_addr eloc, uint32_t elen)
  1945. {
  1946. struct extent_position oepos;
  1947. int adsize;
  1948. int8_t etype;
  1949. struct allocExtDesc *aed;
  1950. struct udf_inode_info *iinfo;
  1951. if (epos.bh) {
  1952. get_bh(epos.bh);
  1953. get_bh(epos.bh);
  1954. }
  1955. iinfo = UDF_I(inode);
  1956. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  1957. adsize = sizeof(struct short_ad);
  1958. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  1959. adsize = sizeof(struct long_ad);
  1960. else
  1961. adsize = 0;
  1962. oepos = epos;
  1963. if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
  1964. return -1;
  1965. while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
  1966. udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
  1967. if (oepos.bh != epos.bh) {
  1968. oepos.block = epos.block;
  1969. brelse(oepos.bh);
  1970. get_bh(epos.bh);
  1971. oepos.bh = epos.bh;
  1972. oepos.offset = epos.offset - adsize;
  1973. }
  1974. }
  1975. memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
  1976. elen = 0;
  1977. if (epos.bh != oepos.bh) {
  1978. udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
  1979. udf_write_aext(inode, &oepos, &eloc, elen, 1);
  1980. udf_write_aext(inode, &oepos, &eloc, elen, 1);
  1981. if (!oepos.bh) {
  1982. iinfo->i_lenAlloc -= (adsize * 2);
  1983. mark_inode_dirty(inode);
  1984. } else {
  1985. aed = (struct allocExtDesc *)oepos.bh->b_data;
  1986. le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
  1987. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1988. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
  1989. udf_update_tag(oepos.bh->b_data,
  1990. oepos.offset - (2 * adsize));
  1991. else
  1992. udf_update_tag(oepos.bh->b_data,
  1993. sizeof(struct allocExtDesc));
  1994. mark_buffer_dirty_inode(oepos.bh, inode);
  1995. }
  1996. } else {
  1997. udf_write_aext(inode, &oepos, &eloc, elen, 1);
  1998. if (!oepos.bh) {
  1999. iinfo->i_lenAlloc -= adsize;
  2000. mark_inode_dirty(inode);
  2001. } else {
  2002. aed = (struct allocExtDesc *)oepos.bh->b_data;
  2003. le32_add_cpu(&aed->lengthAllocDescs, -adsize);
  2004. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  2005. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
  2006. udf_update_tag(oepos.bh->b_data,
  2007. epos.offset - adsize);
  2008. else
  2009. udf_update_tag(oepos.bh->b_data,
  2010. sizeof(struct allocExtDesc));
  2011. mark_buffer_dirty_inode(oepos.bh, inode);
  2012. }
  2013. }
  2014. brelse(epos.bh);
  2015. brelse(oepos.bh);
  2016. return (elen >> 30);
  2017. }
  2018. int8_t inode_bmap(struct inode *inode, sector_t block,
  2019. struct extent_position *pos, struct kernel_lb_addr *eloc,
  2020. uint32_t *elen, sector_t *offset)
  2021. {
  2022. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  2023. loff_t lbcount = 0, bcount = (loff_t) block << blocksize_bits;
  2024. int8_t etype;
  2025. struct udf_inode_info *iinfo;
  2026. iinfo = UDF_I(inode);
  2027. if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
  2028. pos->offset = 0;
  2029. pos->block = iinfo->i_location;
  2030. pos->bh = NULL;
  2031. }
  2032. *elen = 0;
  2033. do {
  2034. etype = udf_next_aext(inode, pos, eloc, elen, 1);
  2035. if (etype == -1) {
  2036. *offset = (bcount - lbcount) >> blocksize_bits;
  2037. iinfo->i_lenExtents = lbcount;
  2038. return -1;
  2039. }
  2040. lbcount += *elen;
  2041. } while (lbcount <= bcount);
  2042. /* update extent cache */
  2043. udf_update_extent_cache(inode, lbcount - *elen, pos);
  2044. *offset = (bcount + *elen - lbcount) >> blocksize_bits;
  2045. return etype;
  2046. }
  2047. udf_pblk_t udf_block_map(struct inode *inode, sector_t block)
  2048. {
  2049. struct kernel_lb_addr eloc;
  2050. uint32_t elen;
  2051. sector_t offset;
  2052. struct extent_position epos = {};
  2053. udf_pblk_t ret;
  2054. down_read(&UDF_I(inode)->i_data_sem);
  2055. if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
  2056. (EXT_RECORDED_ALLOCATED >> 30))
  2057. ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
  2058. else
  2059. ret = 0;
  2060. up_read(&UDF_I(inode)->i_data_sem);
  2061. brelse(epos.bh);
  2062. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
  2063. return udf_fixed_to_variable(ret);
  2064. else
  2065. return ret;
  2066. }