inode.c 62 KB

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