inode.c 61 KB

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