@@ -1244,3 +1244,163 @@ int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
12441244
12451245 return count ;
12461246}
1247+
1248+ static int exfat_get_volume_label_dentry (struct super_block * sb ,
1249+ struct exfat_entry_set_cache * es )
1250+ {
1251+ int i ;
1252+ int dentry = 0 ;
1253+ unsigned int type ;
1254+ struct exfat_sb_info * sbi = EXFAT_SB (sb );
1255+ struct exfat_hint_femp hint_femp ;
1256+ struct exfat_inode_info * ei = EXFAT_I (sb -> s_root -> d_inode );
1257+ struct exfat_chain clu ;
1258+ struct exfat_dentry * ep ;
1259+ struct buffer_head * bh ;
1260+
1261+ hint_femp .eidx = EXFAT_HINT_NONE ;
1262+ exfat_chain_set (& clu , sbi -> root_dir , 0 , ALLOC_FAT_CHAIN );
1263+
1264+ while (clu .dir != EXFAT_EOF_CLUSTER ) {
1265+ for (i = 0 ; i < sbi -> dentries_per_clu ; i ++ , dentry ++ ) {
1266+ ep = exfat_get_dentry (sb , & clu , i , & bh );
1267+ if (!ep )
1268+ return - EIO ;
1269+
1270+ type = exfat_get_entry_type (ep );
1271+ if (hint_femp .eidx == EXFAT_HINT_NONE ) {
1272+ if (type == TYPE_DELETED || type == TYPE_UNUSED ) {
1273+ hint_femp .cur = clu ;
1274+ hint_femp .eidx = dentry ;
1275+ hint_femp .count = 1 ;
1276+ }
1277+ }
1278+
1279+ if (type == TYPE_UNUSED ) {
1280+ brelse (bh );
1281+ goto not_found ;
1282+ }
1283+
1284+ if (type != TYPE_VOLUME ) {
1285+ brelse (bh );
1286+ continue ;
1287+ }
1288+
1289+ memset (es , 0 , sizeof (* es ));
1290+ es -> sb = sb ;
1291+ es -> bh = es -> __bh ;
1292+ es -> bh [0 ] = bh ;
1293+ es -> num_bh = 1 ;
1294+ es -> start_off = EXFAT_DEN_TO_B (i ) % sb -> s_blocksize ;
1295+
1296+ return 0 ;
1297+ }
1298+
1299+ if (exfat_get_next_cluster (sb , & (clu .dir )))
1300+ return - EIO ;
1301+ }
1302+
1303+ not_found :
1304+ if (hint_femp .eidx == EXFAT_HINT_NONE ) {
1305+ hint_femp .cur .dir = EXFAT_EOF_CLUSTER ;
1306+ hint_femp .eidx = dentry ;
1307+ hint_femp .count = 0 ;
1308+ }
1309+
1310+ ei -> hint_femp = hint_femp ;
1311+
1312+ return - ENOENT ;
1313+ }
1314+
1315+ int exfat_read_volume_label (struct super_block * sb , struct exfat_uni_name * label_out )
1316+ {
1317+ int ret , i ;
1318+ struct exfat_sb_info * sbi = EXFAT_SB (sb );
1319+ struct exfat_entry_set_cache es ;
1320+ struct exfat_dentry * ep ;
1321+
1322+ mutex_lock (& sbi -> s_lock );
1323+
1324+ memset (label_out , 0 , sizeof (* label_out ));
1325+ ret = exfat_get_volume_label_dentry (sb , & es );
1326+ if (ret < 0 ) {
1327+ /*
1328+ * ENOENT signifies that a volume label dentry doesn't exist
1329+ * We will treat this as an empty volume label and not fail.
1330+ */
1331+ if (ret == - ENOENT )
1332+ ret = 0 ;
1333+
1334+ goto unlock ;
1335+ }
1336+
1337+ ep = exfat_get_dentry_cached (& es , 0 );
1338+ label_out -> name_len = ep -> dentry .volume_label .char_count ;
1339+ if (label_out -> name_len > EXFAT_VOLUME_LABEL_LEN ) {
1340+ ret = - EIO ;
1341+ exfat_put_dentry_set (& es , false);
1342+ goto unlock ;
1343+ }
1344+
1345+ for (i = 0 ; i < label_out -> name_len ; i ++ )
1346+ label_out -> name [i ] = le16_to_cpu (ep -> dentry .volume_label .volume_label [i ]);
1347+
1348+ exfat_put_dentry_set (& es , false);
1349+ unlock :
1350+ mutex_unlock (& sbi -> s_lock );
1351+ return ret ;
1352+ }
1353+
1354+ int exfat_write_volume_label (struct super_block * sb ,
1355+ struct exfat_uni_name * label )
1356+ {
1357+ int ret , i ;
1358+ struct exfat_sb_info * sbi = EXFAT_SB (sb );
1359+ struct inode * root_inode = sb -> s_root -> d_inode ;
1360+ struct exfat_entry_set_cache es ;
1361+ struct exfat_chain clu ;
1362+ struct exfat_dentry * ep ;
1363+
1364+ if (label -> name_len > EXFAT_VOLUME_LABEL_LEN )
1365+ return - EINVAL ;
1366+
1367+ mutex_lock (& sbi -> s_lock );
1368+
1369+ ret = exfat_get_volume_label_dentry (sb , & es );
1370+ if (ret == - ENOENT ) {
1371+ if (label -> name_len == 0 ) {
1372+ /* No volume label dentry, no need to clear */
1373+ ret = 0 ;
1374+ goto unlock ;
1375+ }
1376+
1377+ ret = exfat_find_empty_entry (root_inode , & clu , 1 , & es );
1378+ }
1379+
1380+ if (ret < 0 )
1381+ goto unlock ;
1382+
1383+ ep = exfat_get_dentry_cached (& es , 0 );
1384+
1385+ if (label -> name_len == 0 && ep -> dentry .volume_label .char_count == 0 ) {
1386+ /* volume label had been cleared */
1387+ exfat_put_dentry_set (& es , 0 );
1388+ goto unlock ;
1389+ }
1390+
1391+ memset (ep , 0 , sizeof (* ep ));
1392+ ep -> type = EXFAT_VOLUME ;
1393+
1394+ for (i = 0 ; i < label -> name_len ; i ++ )
1395+ ep -> dentry .volume_label .volume_label [i ] =
1396+ cpu_to_le16 (label -> name [i ]);
1397+
1398+ ep -> dentry .volume_label .char_count = label -> name_len ;
1399+ es .modified = true;
1400+
1401+ ret = exfat_put_dentry_set (& es , IS_DIRSYNC (root_inode ));
1402+
1403+ unlock :
1404+ mutex_unlock (& sbi -> s_lock );
1405+ return ret ;
1406+ }
0 commit comments